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

How would you make an on touch script only activate when a humanoid touches it (and not any part)?

Sorry for the spam.

I am making a vent that unanchors itself when it is touched, but the script automatically enables itself because it is touching the surrounding walls. Here's what I have so far:

fall = script.Parent
fall.Touched:Connect(function()
	fall.Transparency = 0
	fall.CanCollide = true
	for _, object in pairs(fall.Parent:GetChildren()) do
    object.Anchored = false
end
end)

How would you make it so that the union only unanchors when a player or humanoid touches it and not all parts? Hopefully that made sense.

1 answer

✓ Accepted answer

If you want to check if the thing that touches it is any humanoid, you can use FindFirstChildOfClass to see if there is a Humanoid inside the parent of the thing that touched it:

script.Parent.Touched:Connect(function(Hit)
	if Hit.Parent:FindFirstChildOfClass("Humanoid") then
		print("Touched by a part from a model with a humanoid inside!")
	end
end)

However, if you only wanted to check if the thing that touched was a character of a player, you can use GetPlayerFromCharacter, which will return nil if there is no player that owns the model:

script.Parent.Touched:Connect(function(Hit)
	local Plr = game.Players:GetPlayerFromCharacter(Hit.Parent)
	if Plr then
		print("Touched by " .. Plr.Name)
	end
end)

Hope this helps!

1 comment

0

This isn't an answer, but I vaguely remember trying to make a button and putting Humanoid into a parenthesis. It made the button only work when stepped on by a humanoid. Beginner's luck, I guess.

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