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

How do you add a debounce to a Clickdetector?

Hey, I was wondering about adding a debounce script to my script. The script is basically when a "player" clicks a part they get +1 Money But... there's something about the denounce that doesn't work I get no errors in the console but when I click it doesn't work.

Thanks if someone knows how to do this :D

local debounce = false
local Button = script.Parent.ClickDetector
Button.MouseClick:Connect(function()
	if not debounce then
		debounce = true
		
		script.Parent.ClickDetector.MouseClick:Connect(function(player)
			local PlayerMoney = player.leaderstats.Money
			PlayerMoney.Value = PlayerMoney.Value +1
		end)
		
		
		
		wait(5) -- Cool Down Time
		debounce = false 
	end
end)

2 answers

✓ Accepted answer

Try this:

local debounce = false
local Button = script.Parent.ClickDetector
Button.MouseClick:Connect(function()

        script.Parent.ClickDetector.MouseClick:Connect(function(player)
   	 if not debounce then
        	debounce = true
                local PlayerMoney = player.leaderstats.Money
            PlayerMoney.Value = PlayerMoney.Value + 1
	    wait(5) -- Cool Down Time
        debounce = false 
        end)
    end
end)

1 comment

0

You had to put the debounce inside the event.

You're trying to connect the same event twice inside of each other that's not how you should be doing it, you should have one event with all of the debounce and code, it would look something like this

local debounce = false
local Button = script.Parent.ClickDetector

Button.MouseClick:Connect(function(player)
	if not debounce then

		debounce = true

	       local PlayerMoney = player.leaderstats.Money
	       PlayerMoney.Value = PlayerMoney.Value +1

	       wait(5) -- Cool Down Time
	       debounce = false

	end
end)

Other than that you're pretty much good to go!

1 comment

0

Thank you, I will keep this in mind ;)

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