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

How do I format a countdown like HH:MM:SS?

Asked by
YTWolFs 11
3 years ago

Hello! How do I make so that this countdown script formats the text in Hours:Minutes:Seconds instead of just seconds?

1local GUI = script.Parent
2local ClickDetector = game.Workspace.CDStart.ClickDetector
3 
4ClickDetector.MouseClick:Connect(function(clicked)
5    for i = 120, 0 , -1 do
6        GUI.Text = i
7        wait(1)
8    end
9end)

1 answer

Log in to vote
0
Answered by 3 years ago

Not in the mood to explain, here's a way to get the format from a number.

01function totime(number)
02    local hours, minutes, seconds
03    hours = math.floor(number / 3600); number -= hours * 3600
04    minutes = math.floor(number / 60); number -= minutes * 60
05    seconds = number
06 
07    if hours < 10 then hours = "0"..hours end
08    if minutes < 10 then minutes = "0"..minutes end
09    if seconds < 10 then seconds = "0"..seconds end
10 
11    return hours..":"..minutes..":"..seconds
12end

Put it at the top of your script, and on line 06 do Gui.Text = totime(i).

Ad

Answer this question