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

How do you get the character?

Asked by 9 years ago

Its easy to do if I tried doing that if my script was in the player... but what if its in workspace? If I had a damage script, I need to get the player that it touched.

1 answer

Log in to vote
1
Answered by
Perci1 4988 Trusted Moderation Voter Community Moderator
9 years ago

There's a method of the Players service called GetPlayerFromCharacter(). We provide the character in the parentheses.

It's rather self-explanatory -- it literally attempts to find the Player that is controlling the given character. If the player exists, it returns the player, otherwise it returns nil.

This makes it easy to get the player when using Touched events because they have a built in parameter equal to whatever did the touching. If touched by a user, the Parent of that part will be the character.

script.Parent.Touched:connect(function(hit)
    print(game.Players:GetPlayerFromCharacter(hit.Parent))
end)
Ad

Answer this question