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

How to return player from Touched event?

Asked by
JJ_B 250 Moderation Voter
8 years ago

I made a part that checks if a player is in a group and then allows that person entry if they are, but i don't know how to get the player from the touched. Here is what I DID do:

script.Parent.Touched:connect(function(hit)
    if hit.Parent:IsInGroup(2683043) then
        script.Parent.CanCollide = false
        wait(0.8)
        script.Parent.CanCollide = true
    end
end)

Help?

1 answer

Log in to vote
1
Answered by 8 years ago

The Touched event returns the actual Part that touched script.Parent. This Part could be a random Part, a leg, arm, torso, etc.. To check whether a player's character touched it, you need to do this:

if hit and hit.Parent:FindFirstChild('Humanoid') then
    -- code
end

Now, you are calling IsInGroup on the Character that contains that Part, but you have to call it on the Player. To get the actual Player, do this:

player = game.Players:GetPlayerFromCharacter(hit.Parent)

And now, you can check if that player is in group. Of course, there are other methods to do this, but this is the one that came to my mind.

Hope this helped.

Ad

Answer this question