Help with making a countdown

Hi, I need some help in making a countdown timer.

Here’s my current script:

game.Workspace.Audio.Music.CashBuilderWithSiren:Play()

wait(0.5)

for i = 59, 1, -1 do
    script.Parent.Countdown.Text = "0:"..i
    wait(1)
end

The only issue I’m having is when it gets down to single numbers, for example 4, the text is changing to 0:4, when I need it to be 0:04

How can I achieve this?
Ty in advance :grinning:

1 Like

Try this

game.Workspace.Audio.Music.CashBuilderWithSiren:Play()

task.wait(0.5)

for i = 59, 1, -1 do
	if i > 10 then
		script.Parent.Countdown.Text = "0:"..i
		task.wait(1)
	elseif i <= 10 then
		script.Parent.Countdown.Text = "0:0"..i
		task.wait(1)
	end
end
2 Likes

that works! tysm!

although, how can I make it so when the countdown reaches 0 the text changes to 0:00

So once it hits zero the timer will be 0:00 and the loop will break.


game.Workspace.Audio.Music.CashBuilderWithSiren:Play()

task.wait(0.5)

for i = 59, 1, -1 do
	if i > 10 then
		script.Parent.Countdown.Text = "0:"..i
		task.wait(1)
	elseif i <= 10 and i > 0 then
		script.Parent.Countdown.Text = "0:0"..i
		task.wait(1)
	elseif i <= 0 then
		script.Parent.Countdown.Text = "0:00"
	end
end

ok so the only issue with this is when it hits ten, the text is 0:010, when it should be 0:10
and the countdown never changed the text to 0:00 it stayed at 0:01

my bad.

game.Workspace.Audio.Music.CashBuilderWithSiren:Play()

task.wait(0.5)

for i = 59, 1, -1 do
	if i > 10 then
		script.Parent.Countdown.Text = "0:"..i
		task.wait(1)
	elseif i < 10 and i > 0 then
		script.Parent.Countdown.Text = "0:0"..i
		task.wait(1)
	elseif i <= 0 then
		script.Parent.Countdown.Text = "0:00"
	end
end

Still does not work. It skips 10, so it goes from 11 to 9, and also does stop at 0:00, it stays at 0:01

Okay I reviewed the script and made sure to test it and this works for me.
Big apologies just had a long day so I didn’t realize my error.

game.Workspace.Audio.Music.CashBuilderWithSiren:Play()

task.wait(0.5)

for i = 59, 0, -1 do
	if i >= 10 then
		script.Parent.Countdown.Text = "0:"..i
		task.wait(1)
	elseif i < 10 and i > 0 then
		script.Parent.Countdown.Text = "0:0"..i
		task.wait(1)
	elseif i <= 0 then
		script.Parent.Countdown.Text = "0:00"
        task.wait(1)
	end
end
2 Likes

it works!!! tysm, your a lifesaver :grinning::grinning:

I know that this already been solved, but I just wanted to let you know that it’s possible without the use of if-else statements. You could instead use the string.format() function like this:

print(string.format("00:%02D", 4))  -- 00:04
print(string.format("00:%02D", 30)) -- 00:30
for i = 59, 0, -1 do
	script.Parent.Countdown.Text = string.format("0:%02D", i)
	task.wait(1)
end

If you also want to include the minutes:

for minutes = 4, 0, -1 do
	for seconds = 59, 0, -1 do
		script.Parent.Countdown.Text = string.format("%02D:%02D", minutes, seconds)
		task.wait(1)
	end
end
2 Likes

That works also, forgot that was a thing.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.