Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to return player from Touched event?

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

✓ Accepted answer

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.