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

Roblox Touched Event And TouchEnded Event Broken Please Help?

local part = script.Parent

part.Touched:Connect(function(hit)
		local H = hit.Parent:FindFirstChild("Humanoid")
				if H ~= nil then
					print("player is in")
				end
end)

part.TouchEnded:Connect(function(hitended)
		local H = hitended.Parent:FindFirstChild("Humanoid")
				if H ~= nil then
					print("player is not in")
				end	
end)

if I touched the part and not moving it prints "player is in" and "player is not in" I don't know how to fix the problem also if I moved it spams "player is in" and "player is not in" I don't know if touchedended and touched is working! please Help!

1 answer

In my opinion, using those systems in big detector parts is problematic without a cooldown. So I suggest you use a cooldown system which is very easy to make.

EDIT: I forgot the thing that, even with the cooldown thing, it will still spam-print the text. The reason is that the function ends while the player is still in, so we need the function to yield until the player leaves. In this case, I will remove the TouchEnded event, as it won't be necessary.

local part = script.Parent

part.Touched:Connect(function(hit)
	local H = hit.Parent:FindFirstChild("Humanoid")
        if H ~= nil then
       		print("player is in")
        end
	repeat wait() until not H
end)

part.TouchEnded:Connect(function(hit)
	local H = hit.Parent:FindFirstChild("Humanoid")
        if H ~= nil then
       		print("player is not in")
        end
end)

If this still doesn't work, I'd suggest using Region3 instead of Touched Events

3 comments

0

If it doesn't work, please tell me, as I might have forgotten some things in the script.

0

Ain't working :C

0

I see, let me edit the answer a bit

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