Touching script not working correctly

Hey everyone! I’m trying to make a script that detects if the player is touching a part and then turn a bool value to true, or false if he is not touching it. Anyways, the Touch function seems to be very buggy, as the player continues touching it, but the TouchEnded is still fired, and I got no idea how to make it work as intended.

Here’s the script:

local Verifier = script.Parent.Parent.Verifier
local CanAccess = script.Parent
local Debounce = false

Verifier.Touched:Connect(function(Enable)
	if Enable.Parent:FindFirstChildOfClass("Humanoid") then
		if Debounce then return end
		Debounce = true
		print("PC: ON")
		CanAccess.Value = true
	end
end)

Verifier.TouchEnded:Connect(function()
	print("PC: OFF")
	CanAccess.Value = false
	wait(.2)
	Debounce = false
end)

This can save lives


for i,v in pairs(Verifier:GetTouchingParts()) do

if game.Players:GetPlayerFromCharacter(v.Parent) then

return end

end

1 Like

Well, if it does then I think I’m already dead :sad:
For some reason the code still doesn’t work, actually nothing happens when I execute it now.

Did you try rearranging it at all?

Also, adding a return in somewhere can help you exit a function early. I edited my last comment as an example lol

I don’t know what you mean. And I got no idea how I should proceed because not even errors are being showed in output

The code now is looking like this:
(maybe is my fault I forgot to mention that I suck at scripting)

local Verifier = script.Parent.Parent.Verifier
local CanAccess = script.Parent
local Debounce = false

for i,v in pairs(Verifier:GetTouchingParts()) do
if game.Players:GetPlayerFromCharacter(v.Parent) then

print("do something")

return end
end

You should have put that inside of your Touched and TouchEnded scripts somewhere so they run each time one of those things happen.

What you just showed me only runs once lmao

LOL.
Yeah, now I managed to make the script work again, but it’s still pretty much the same. The script says when I’m not touching when I still am, and sometimes the TouchEnded is not triggered