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 cooldown/Delay for pressing a Gui button?

Hello, I making an Fps game so How do you add a cooldown/Delay so the players don't abuse the system there is an image button that's once pressed it shows up a Gui frame, the button scripting is quite simple

script.Parent.MouseButton1Click:Connect(function()
	script.Parent.Parent.Frame.Visible = true
end) --Please Note this is a **local script**

So how do you add a 30 secs or a minute cooldown until you can access it again? and if the player tries to press the button while the button is in cooldown a script will play [Sound] which is this game.SoundService.Barret_Equip.Playing = true and when the button is NOT on cooldown the sound plays game.SoundService.Bell.Playing = true Sorry if I am asking for too much

3 answers

✓ Accepted answer
local debounce = false
script.Parent.MouseButton1Click:Connect(function()
    if debounce == true then 
		game.SoundService.Barret_Equip.Playing = true
		return
    else 
		game.SoundService.Bell.Playing = true
    end
   	 script.Parent.Parent.Frame.Visible = true
    	wait(30) -- change to 60 if you want it to be a minute
   	 debounce = false
end) 

This should be what you are looking for. The debounce is just a variable that is set as false originally, until it is first clicked. It is then set back to false after 30 seconds.

I recommend adding something called a debounce. It goes like this:

local debounce = false

script.Parent.MouseButton1Click:Connect(function()
if debounce == false then
	debounce = true
	script.Parent.Parent.Frame.Visible = true
wait(30)
debounce = false
	end
end)

Anything you may wish to add to that, you can add before the "wait(30)" and after the "debounce = true"

Hope this helped!

4 comments

0

Yes this helped alot, but where do you put the else statement so if the player presses while the cooldown is on a sound plays?

0

I stated that below my block of code

0

I’m sorry if I’m just batting in but does this work on any GUI?

0

Yes

Really easy to do! You can add a denounce and name it whatever you want! It basically stops the code from executing when it is running at a moment.

local debounce = false

script.Parent.MouseButton1Click:Connect(function()
    if not debounce then -- Checks if debounce if false
    debounce = true -- Sets it to true
    script.Parent.Parent.Frame.Visible = true
     wait(2)
     debounce = true
        end
end) --Please Note this is a **local script**

1 comment

0

Thank you for your comment, but DarkDanny04 answered sooner than you... But The script still works ;)

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