Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to stop a wait() and restart it?

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.

2 comments

0

wat

0

Use a for loop that goes up to 10 and you wait(1) inside that loop. But before that you should have a variable named "isClicked" or something. Set it to true when clicked.

1 answer

✓ Accepted answer

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.

2 comments

0

Thank you!

0

Made some edits please refresh for a better version :)

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