Hello,
I am working on a gui based game. I want a gui button to have a cooldown after being pressed.
Here is what I tried (you can ignore most of it, I'm just concerned about the debounce).
01 | function clicked(plr) |
02 | debounce = true |
03 | if debounce = = true then |
04 | debounce = false |
05 | local player = game.Players.LocalPlayer |
06 | local edulvl = player.Education.Value |
07 | local x = game.ReplicatedStorage.Work:InvokeServer(edulvl) |
08 | local y = text(edulvl) |
09 | script.Parent.Parent.Parent.Status.Visible = true |
10 | script.Parent.Parent.Parent.Status.TextLabel.Text = (y.. " " ..x.. " dollars" ) |
11 | wait( 5 ) |
12 | script.Parent.Parent.Parent.Status.Visible = false |
13 | debounce = true |
14 | end |
15 | end |
I thought I had my debounce set up correctly, but it doesn't seem to work.
Help would be appreciated
Define debounce outside the click function because you are basically resetting the debounce, setting it to true upon clicking the button, thus removing the cooldown
01 | local debounce = true |
02 | function clicked(plr) |
03 | if debounce = = true then |
04 | debounce = false |
05 | local player = game.Players.LocalPlayer |
06 | local edulvl = player.Education.Value |
07 | local x = game.ReplicatedStorage.Work:InvokeServer(edulvl) |
08 | local y = text(edulvl) |
09 | script.Parent.Parent.Parent.Status.Visible = true |
10 | script.Parent.Parent.Parent.Status.TextLabel.Text = (y.. " " ..x.. " dollars" ) |
11 | wait( 5 ) |
12 | script.Parent.Parent.Parent.Status.Visible = false |
13 | debounce = true |
14 | end |
15 | end |
Remove the debounce = true
on line 2, and define it outside the function. You are resetting the debounce every time you click it which essentially counteracts the point of the debounce.
01 | local debounce = true |
02 | local function clicked(plr) |
03 | if debounce then |
04 | debounce = false |
05 |
06 | -- code goes here |
07 |
08 | debounce = true |
09 | end |
10 | end |
Hope this helps! :)