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

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

Asked by 10 years ago

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

Log in to vote
0
Answered by
Noculus 25
10 years ago

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
Ad
Log in to vote
1
Answered by
User#2 0
10 years ago

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.

0
Thanks! MINECRAFTERGUY 10 — 10y
Log in to vote
0
Answered by 10 years ago
game.Players:getPlayerFromCharacter(character)

game.Players["PLAYERNAME"].Character

Answer this question