Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I get the player from the character model in Workspace/Vice versa?

I need to know how to get who the player is (like the one in Players) from the character model in Workspace and vice versa. Is there a way to do this via script?

3 answers

✓ Accepted answer

How to get Player from Character:

player = game.Players:GetPlayerFromCharacter(game.Workspace.hawkspeed)

How to get Character from Player

character = game.Players.hawkspeed.Character
--Character is a child of Player

There are some good ways to do this.

Let's start with getting the Character from the Player.

Player.Character

That was easy! There's a property of the Player object called "Character". This property is, you guessed it, the player's current Character. This will be nil if at that moment the player doesn't have a character (they haven't yet spawned/loaded the game.

Getting the player from the character is ever-so-slightly more challenging, but still possible.

How I normally do it is I search through all of the players in the Players service until one of their Character properties matches the character I'm looking for.

function FindPlayerFromCharacter(Character)
	for i,v in pairs(game.Players:GetPlayers()) do -- For every player do...
		if v.Character == Character then -- If their character is the provided character then..
			return v -- Return that player
		end
	end
	return nil -- No player found with that character, return "nothing"
end

Edit: I totally forgot about the GetPlayerFromCharacter method of the Players service! I'd suggest using that over the function provided above.

1 comment

0

Thanks!

game.Players:getPlayerFromCharacter(character)

game.Players["PLAYERNAME"].Character

This discussion is preserved as it was. New answers, comments, and votes are closed.