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

How to stop a wait() and restart it?

Asked by 5 years ago

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.

0
wat TheeDeathCaster 2368 — 5y
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. User#24403 69 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

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.

0
Thank you! xXrawr_xdXxOwO 24 — 5y
0
Made some edits please refresh for a better version :) KamikazeJAM108867 38 — 5y
Ad

Answer this question