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

Make a countdown clock?

Asked by 9 years ago

so there has to be a better way to do this. I got it starting at 10:00 and it goes down to 00:00. That's ten minutes. But, here's my way of counting down and there has to be a faster way, help?

local gametime = script.Parent

while true do
    wait(1)
    gametime.Text = "09:59"
    wait(1)
    gametime.Text = "09:58"
    wait(1)
    gametime.Text = "09:57"
    wait(1)
    gametime.Text = "09:56"
end

And so on to 00:00

1 answer

Log in to vote
3
Answered by
Redbullusa 1580 Moderation Voter
9 years ago

The bare bones of a countdown timer conventionally consist of a 'for' loop running backwards.

gametime = script.Parent
totalsec = 600 -- Because 10 minutes * 60 seconds per minute = 600 seconds

for i = totalsec, 0, -1 do
    gametime.Text = i
    wait(1)
end

This will only display the amount of seconds left, though - not in MM:SS format.

MM:SS FORMAT:

05:30

NOT MM:SS FORMAT:

330

To do so requires the knowledge of the modulo operator (%). A modulo operation returns the remainder in a division operation.

Example:

11 / 5 = 10 r 1

11 % 5 = 1

So, how do you implement this knowledge into the countdown timer? By:

  • Assigning more variables (minutes and seconds)

  • Get the amount of minutes left

  • Get the amount of seconds left without breaching the 59-second boundary


Stick to the first script if you don't really understand the modulo operator for now.


MODULO OPERATION EXAMPLE

For five minutes, this example will print the time counting up.

totalsec = 300                      -- 5 minutes

for i = 0, totalsec do              -- timer counts up
    local Min = math.floor( i / 60 )
    local Sec = i % 60

    if Min < 10 then                    -- If minute is less than 10, add a "0," so: "05" instead of "5."
        Min = ("0".. Min)
    end
    if Sec < 10 then                    -- If second is less than 10, add a "0," so: "08" instead of "8."
        Sec= ("0".. Sec)
    end

    print(Min .. ":" .. Sec)
    wait(1)
end
Ad

Answer this question