Create a countdown clock

Hey developers!

I am here to ask how to make a countdown clock, for example:

Instead of 90 - 0 I want 00:01:20 - 00:00:00

Can anyone tell me how to accomplish this?

1 Like

I actually remember posting something about this, would you want it to replicate across all clients ingame? Cause it’s pretty simple to do server/client wise :thinking:

It is on a SurfaceGui and I want the whole server to see it.

Ah, well in that Instance you could create 2 Number/IntValues (Minutes & Seconds) inside the SurfaceGui & every second that passes you can check if the Seconds are equal to 0:

local Minutes = script.Minutes
local Seconds = script.Seconds

while Minutes.Value ~= 0 or Seconds.Value ~= 0 do
    Seconds.Value -= 1
    wait(1)
    if Seconds.Value == 0 then
        Minutes.Value -= 1
        Seconds.Value = 60
    end

end

I haven’t tested this out but I think this should work?

1 Like

It doesn’t work to well, when it reached 0 it just stops and when it goes under the value of 10 the 0 vanishes.

Ill send an example of what it does.

You can use String Patterns to simplify the task:

local function ToTimerFormat(Seconds)
	local Minutes = Seconds / 60
	local Hours = Seconds / 3600
	
	return string.format("%02d:%02d:%02d", Hours, Minutes % 60, Seconds % 60)
end

%d pattern stands for digit, if you add %02d to it, it will add zeros to the number if it has only one digit, turning 9 into 09 for example. % (Modulus) operator is used to turn digits that are above 60 into a 0 to prevent the timer from showing 123 seconds but 60 max.

With this function can use simple for loop:

for Time = 90, 0, -1 do
	print(ToTimerFormat(Time))
	wait(1)
end

I reccomend you taking delta time into account too when making timers.

6 Likes

Ok that works perfectly! Thank you so much!