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

How do I get 'player' from a touch event?

Asked by 4 years ago

I need to access the player gui to enable a message to be displayed on their screen when they touch a part but I'm unsure how to do it. Can anyone please help?

Normally, you can access the character without a problem but I don't know how to get the player.

2 answers

Log in to vote
3
Answered by
Nanomatics 1160 Moderation Voter
4 years ago

The GetPlayerFromCharacter function returns the Player associated with the given character.

So to get the player from a touched event you would do something like this:

local part = workspace.Part
part.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then -- part belongs to character
        local player = game.Players:GetPlayerFromCharacter(hit.Parent)
    end
end)

That's it, you got your player.

Hope I helped, if you have any questions please let me kno1!

0
Thanks so much! iiPizzaCraver 71 — 4y
Ad
Log in to vote
0
Answered by
JesseSong 3916 Moderation Voter Community Moderator
4 years ago

Please watch this video first https://www.youtube.com/watch?v=4beMzJ1GInI

Answer this question