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

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

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

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

1 comment

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

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