Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Can someone explain GetPlayerFromCharacter()??

Asked by 10 years ago

What is this inbuilt function???

1 answer

Log in to vote
3
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
10 years ago

A "Player" in your place has essentially two components.

The Player object which resides in the Players service. This contains things like userId, TeamColor, Name, and CharacterAppearance.

The other component is the Character, which is the physical model that resides in the Workspace service. This contains the Torso, Head, Legs, Arms, and Hats, Shirts, Pants, equipped Tools, etc.


All Player objects have a .Character property which lets you get the Character model from the Player model.

Since the Character is just a regular Model instance (in the workspace) it has no direct way to figure out which Player it belongs to (the reverse of the above)

So, ROBLOX provides the method of the players service Players:GetPlayerFromCharacter( someCharacter ) which returns the Player object corresponding to the given someCharacter Character model.

If the model given isn't actually a Character of one of the players, then :GetPlayerFromCharacter will return nil.


Here are some simple properties (assuming you haven't done anything weird to your place):

game.Players:GetPlayerFromCharacter( Player.Character ) == Player

Player.Character.Name == Player.Name

Player.Parent = game.Players
Player.Character.Parent = game.Workspace

game.Players[ Character.Name ] == game:GetPlayerFromCharacter( Character )
-- I the event somehow there are 2 players
-- with the same name (as old Test Server used) this will
-- not work correctly.
-- In the event there are no players with this name, this will *ERROR*
Ad

Answer this question