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

How to loop a countdown when it's ended? [GUI]

Asked by 4 years ago
Edited 4 years ago

I'm kinda stuck. If you could answer on what to delete/add, that would be highly appreciated.

I'm trying to make a countdown, and when the countdown reaches 0, the countdown waits 5 seconds and then resets/restarts the countdown.

The script is placed inside of the TextLabel.

local time = 150 --The time it starts at.

for i = 1, 150 do

wait(1)

time = time - 1

script.Parent.Text = tostring(time)

end

script.Parent.Text = "Game over."

if script.Parent.Text == 0 then

wait(5)

script.Parent.Text = "150"

end

This is all I have so far. Any help would be appreciated!

1 answer

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

So What I would say is don't put the code in a for loop but a while true loop if you want it to go on forever

Old Code Here is what you originally have

local time = 150 --The time it starts at. 

for i = 1, 150 do 
    wait(1) 
    time = time - 1
    script.Parent.Text = tostring(time) 
end 

script.Parent.Text = "Game over." 

if script.Parent.Text == 0 then 
    wait(5) 
    script.Parent.Text = "150" 
end

Now if you wat the code to go on forever here is a method that you can use to do it

local startTime = 150 --The time it starts at. 
local currentTime = startTime-- Makes it so only one value has to be changed to change the time length

while true do
    script.Parent.Text = tostring(currentTime) --Set the text equal to the time

    if currentTime<= 0 then--check if its zero
        script.Parent.Text = "Game over." 
        wait(5) 
        currentTime = startTime
    else--if not subtract one
    currentTime currentTime time - 1
    end

    wait(1) --wait one second

end
0
Its not perfect and you can change things as needed or rearange them as you want but that should help give you a rough idea on how to do it Good luck with your project deth836231 142 — 4y
Ad

Answer this question