So I have a button and I want it to display a GUI for 10 seconds when clicked, but I also want the timer to reset when the button is clicked again and the 10 seconds aren't finished yet.
Like when someone is spamming the button the timer keeps going back to 10 seconds and the GUI is still visible but when they stop clicking for 10 seconds the GUI goes invisible. I know how to do the GUI stuff but I have no idea how to have a timer that stops then resets.
Here is some code I've created that may help:
I used a variable called "timeleft" to store how long the player has left
local timeleft = 10 local button = script.Parent local gui = script.Parent.Parent.Frame button.MouseButton1Click:Connect(function() timeleft = 11 end) while true do timeleft = timeleft - 1 script.Parent.Text = tostring(timeleft) wait(1) end
The above code simply resets the timer every click. Since you want the GUI to become invisible when the timer hits 0, you can add an if statement:
local timeleft = 10 local button = script.Parent local gui = script.Parent.Parent.Frame button.MouseButton1Click:Connect(function() timeleft = 11 end) while true do timeleft = timeleft - 1 if timeleft <= 0 then -- checks if timeleft went below 0 and sets gui invisble timeleft = 0 -- prevents negative time gui.Visible = false else gui.Visible = true --sets visible to true if there it time left end script.Parent.Text = tostring(timeleft) wait(1) end
If this helped please accept. Feel free to comment if you need help.