I know how to find it in a LocalScript, but if I have a ServerScript, how do I find it? I have tried this code, but I think what I find is Workspace.
1 | local part = script.Parent |
2 |
3 | local event = part:FindFirstChild( "RemoteEvent" ) |
4 |
5 | part.Touched:connect( function (hit) |
6 | local player = hit.Parent.Parent |
7 | event:FireClient(player, "connect" , hit) |
8 | end ) |
Thank you for any answer!
You can use GetPlayerFromCharacter, or just go into Players to get the player. Example:
1 | script.Parent.Touched:connect( function (part) |
2 | local player = game.Players:GetPlayerFromCharacter(part.Parent.Name) --the reason this works if someone's character touches it is because a child of the character model's parent is the character. like if my character's left leg touches a brick. the left leg's parent would be a model called "yumtaste," and GetPlayerFromCharacter gets the player called "yumtaste" |
3 | if player then --sometimes, an NPC or a random brick can touch the brick that is listening for the Touched event. this makes sure that "player" actually exists, because it might not. |
4 | print (player.Name.. " just touched mah brik! arrests hims nao!!!" ) |
5 | end |
6 | end ) |
You could also do player = game.Players:FindFirstChild(part.Parent.Name)
and still get the player. I just go into Players to find the player because it works just as well, and I just historically used it.
If you have any questions, please feel free to comment!
Upvote and accept