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

How to properly add Debounce to a script? (Easy)

So how do you really add debounce to a script? Explain detailed because i am learning

How would you add debounce to this script and how would it work?

script.Parent.ClickDetector.MouseClick:Connect(function()
	local Sound = script.Parent.Sound
	Sound:Play()
end)

2 answers

✓ Accepted answer

To add a debounce, you can do this:

local Debounce = false -- Debounce variable
local Sound = script.Parent.Sound

script.Parent.ClickDetector.MouseClick:Connect(function()
    if (Debounce) then return end -- If it's already on, do nothing.
    	Debounce = true -- Turn on debounce.
    	Sound:Play()
    	wait(2)
    	Debounce = false -- Turn off debounce.
	end
end)

Hope this helped you.

Here is an example debounce script

local debounce = false

script.Parent.ClickDetector.MouseClick:Connect(function()
	if debounce = false then
		debounce = true
		print("On")
	else
		debounce = false
		print("Off")
	end
end)

You can read more here

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