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

Is there a way to add a cooldown to a button?

Asked by 4 years ago
Edited 4 years ago

I'm trying to make it so when a player clicks a button the button waits for 3 seconds before it can work again but every time I tried to do it if the player spam clicks it the function still gets spammed, is there any way to add a cooldown to when the button can be pressed?

Discord: Unknown Intelligence#6569

01local bool = true
02 
03local Players = game:GetService("Players")
04 
05Players.PlayerAdded:Connect(function(player)
06    local light = game:GetService("Lighting")
07light.TimeOfDay = 1
08light.FogEnd = 50
09light.FogStart = 20
10light.Brightness = 0
11light.FogColor = Color3.new(0,0,0)
12 
13end)
14 
15 
View all 37 lines...
0
Did you try "wait(3)"? lopte1 -5 — 4y
0
Yeah that's what I did and it just waited 3 seconds before bombarding you with the spams xD adamson239 12 — 4y

1 answer

Log in to vote
1
Answered by 4 years ago

In the scripting world, we call this "debounce". Don't ask me why, the Code Gods just made it like that. Before your function, add a variable called "debounce" and set it to false. Then when the function is ran, set the debounce as true with a wait time of three seconds (or whatever you want). It'll look something like what I have below.

01local debounce = false
02 
03function onMouseClick()
04if not debounce then
05    debounce = true
06    if bool == true then
07    local light = game:GetService("Lighting")
08    script.Lightson:Play()
09    light.TimeOfDay = 12
10    light.FogEnd = 1000000
11    light.FogStart = 0
12    bool = false
13        else if bool == false then
14        local light = game:GetService("Lighting")
15    script.Lightsoff:Play()
View all 28 lines...

Sorry if the script is badly formatted and organized, writing this on mobile xD.

0
I KNEW I SHOULD'VE USED DEBOUNCE omg thank you xD adamson239 12 — 4y
Ad

Answer this question