How do you make a Touched event trigger only when another part is touching the object?

local Part = script.Parent

function WhenFall(part)
local humanoid = part.Parent:FindFirstChild(“Humanoid”)
if humanoid then

else

print(“Work”)

end

end

Part.Touched:Connect(WhenFall)

this is my code right now and it isnt working. Anyone have a fix?

2 Likes

Try this:

Humanoid | Roblox Creator Documentation

1 Like

Do you know how i could put that into my script? Im really new to coding so i practically know nothing.

1 Like

Are you suppose to expect the print statement to print out Work? If that’s the case, you put it in the else statement, which never works until there is no humanoid touching the part.

1 Like

No. Im trying to get the print statement to NOT print work when I touch the part, and only when another part touches it. But for some reason it prints if anything is touching the part.

1 Like
local Part = script.Parent

function WhenFall(part)
	local humanoid = part.Parent:FindFirstChild("Humanoid")
	if humanoid then
		print("A model with a humanoid touched!")
	else
		print("Something else touched!")
	end
end

Part.Touched:Connect(WhenFall)

This will work, make sure the other part that triggers the event to fire doesn’t belong to a model which also has a Humanoid instance otherwise it’ll behave in the same way as when you were touching the part with your own character.

This seems to work, but if I remove the print(“A model with a humanoid touched!”) then whenever I touch it, I get “Something else touched!” although im the one touching it.

This is what happens if i touch the brick.

every time i stop touching it and touch it again, i get one “Something else touched!” but if i stay touching the brick i get multiple “You touched” why is this?

For the first point, the reason it “Something else touched!” is because the part named “Handle” inside your hair accessory is touching the blue sphere, fixing that is relatively easy. Regarding the second part, Touched/TouchEnded often fires regularly (even when you wouldn’t expect it to due to the way part collision works in Roblox), you can remedy that by adding a debounce to the callback in order to prevent it from executing too frequently.

local Part = script.Parent
local debounce = false

function WhenFall(part)
	if debounce then
		return
	end
	if part.Parent:IsA("Accessory") then
		return
	end
	local humanoid = part.Parent:FindFirstChild("Humanoid")
	if humanoid then
		debounce = true
		print("A model with a humanoid touched!")
		task.wait(5) --cooldown length
		debounce = false
	else
		print("Something else touched!")
	end
end

Part.Touched:Connect(WhenFall)

oh okay thanks so much! I would of probably never found out that my hair was the problem. Thanks once again!