Thundermaker300 here with a tutorial on Player objects. These objects are essential to games, as they contain properties of an in-game user.
Joining and Leaving
When a player joins a game, a player object is added into the Players service.
The player object is removed when a player leaves the game. When a player object is added to the Players service, game.Players.PlayerAdded is called. When a player object is about to be removed, game.Players.PlayerRemoving is called.
Important Properties of a Player
UserId
The first property I will go through is UserId. Each player, when joining Roblox, has a unique identifier, called the UserId. The UserId can be obtained two different ways. It can be obtained by going to someone's profile, shown in the picture. The second way is by using the UserId property of a Player object.
This is Thundermaker300's UserId.
MembershipType
This property determines the membership this player has. It is an enum, and can either be None, BuildersClub, TurboBuildersClub, OutrageousBuildersClub, or Premium.
Character
This property is a model in Workspace. This is the character of the player that interacts, walks, jumps, etc. For more info on the character's structure, see this page.
Using Properties In Code
We have all these properties, but what should we do with them? There is many different things you can do using these properties. Here are some examples you can do using these.
Kill a Player
game.Players["PLAYERNAME"].Character.Humanoid.Health = 0
This code makes the health set to 0, to kill the player.
Remove the player's Head
game.Players["PLAYERNAME"].Character.Head:Destroy()
This code removes the player's head, another way to kill the player.
Get the player's membership
print(game.Players["PLAYERNAME"].MembershipType)
This code prints the MembershipType of the player in the output.
Print the player's Name and UserId
print(game.Players["PLAYERNAME"].Name.."'s UserId is "..game.Players["PLAYERNAME"].UserId)
This code prints out the player's name and UserId.
| Scripting (44) |
| ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
| GUI (8) |
| ||||||||||
| Building (3) |
| ||||||||||
| Platform (8) |
| ||||||||||