Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How do you add a cooldown/Delay for pressing a Gui button?

Asked by 4 years ago

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

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago
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.

Ad
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

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!

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? Smartics 25 — 4y
0
I stated that below my block of code iiPizzaCraver 71 — 4y
0
I’m sorry if I’m just batting in but does this work on any GUI? iivSnooxy 248 — 4y
0
Yes iiPizzaCraver 71 — 4y
Log in to vote
0
Answered by 4 years ago

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**
0
Thank you for your comment, but DarkDanny04 answered sooner than you... But The script still works ;) Smartics 25 — 4y

Answer this question