When I spam click with the tool it prints “Activated”, then doesn’t print anything for 3 seconds, then it just prints it however many times I click, and completely ignores the debounce. Why is this happening?
local debounce = false
script.Parent.Activated:Connect(function()
if not debounce then
debounce = true
print("Activated")
end
wait(3)
debounce = false
end)
Huh, weird, I had done that before (I think?) and it was spamming, I moved it out, and it then did this thing, I made this post, then moved it back in and it’s now working. Weird.
As @Benified4Life said move the code. Your code will wait then reset the debounce. If multiple threads are waiting the result is the deounce being reset one after the over when they are resumed allowing for the “print(“Activated”)” to run multiple times since debounce will be set to false multiple times.
Often developer just build a wrapper for creating a debounce in a module (it used to be on the wiki under modulescript).
return function(func)
local deb = false
return function(...) -- new function passed back wrapping the main function call
if deb then return end
deb = true
func(...) -- run main function, pass any arguements
deb = false
end
end
Usage
local deb = require([module path])
intValue.Changed:Connect(deb(function(newVal)
wait(3)
print(newVal)
end))