I am writing a script, that triggers when the player steps on something. I set this up to test if it would work.
local touching = false script.Parent.Trigger.Touched:connect(function(hit) if not touching then print("Touched") touching = true end end) script.Parent.Trigger.TouchEnded:connect(function(hit) if touching then print("TouchEnded") touching = false end end)
As a player walks on it, it triggers it multiple times EG, my output window TouchEnded Touched TouchEnded Touched TouchEnded Touched TouchEnded
How can I prevent this from happening?
Use a Debounce. A Debounce prevents the event to happen infinite times.
touching = false --Local not necessary debounce = true script.Parent.Trigger.Touched:connect(function(hit) if not touching and debounce == true then print("Touched") touching = true debounce = false wait(1) end end) script.Parent.Trigger.TouchEnded:connect(function(hit) if touching and debounce == true then print("TouchEnded") touching = false debounce = false wait(1) debounce = true end end)