How do you add debounce in this script? I want it to wait 4 seconds before it does the second part of the coding when touched. So, it does the first action, then wait 4 seconds, next do the second action, and at last wait another 4 seconds before it does the first action again. Thanks!
local boolean = false function onTouched() if boolean then boolean = false --code wait(4) else boolean = true --code wait(4) end end script.Parent.Touched:connect(onTouched)
local boolean = true local action = 1 function onTouched() if boolean == true then boolean = false if action == 1 then action = 2 --action1 wait(4) boolean = true elseif action == 2 then action = 1 --action2 wait(4) boolean = true end end end script.Parent.Touched:connect(onTouched)
Something like this should work.
local boolean = false function onTouched() if not boolean then boolean = true wait(4) --run your code boolean = false end script.Parent.Touched:connect(onTouched)
Here's a practical use for debounce.
local debounce = false script.Parent.Touched:connect(function(hit) if not debounce then debounce = true local hit = hit.Parent local hum = hit:FindFirstChild("Humanoid") hum.Health = hum.Health - 5 wait(3) debounce = false end end)