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

How can i make my countdown variable use the format 0:30?

Asked by 8 years ago
for i = 30,1,-1 do
    for i,v in pairs(game.Players:GetPlayers))) do
        v.PlayerGui.Intermission.Countdown.Text = "Intermission: " ..i
    end
    wait(1)
end

This is the script i create to make the countdown go from 30 to 1, but what if instead of having the number 30 i wanted the format to be like 0:30, or maybe i even wanted 2 whole minutes, how could i make it in the format of 2:00, rather than 120? I have tried almost everything and still could not succeed, could anyone help, show, and explain to me how to do this?

0
If you want to do more than a minute, you'd have to store minutes and seconds in seperate variables. Other than that, you'd have to format the string like ("Intermission: 0:" .. i) or ("Intermission: " .. min .. ":" .. sec). GoldenPhysics 474 — 8y

2 answers

Log in to vote
0
Answered by 8 years ago
Edited 8 years ago

Check out string formatting for an easy way to accomplish this.

string.format("%i:i", seconds/60, seonds%60)

will give you the format you want. For example,


local seconds = 30; print(string.format("%i:i", seconds/60, seconds%60)); --> 0:30 seconds = 120; print(string.format("%i:i", seconds/60, seconds%60)); --> 2:00
0
However, if the seconds are under 10 (take 3 for example), then it will result in 1:3! starlebVerse 685 — 8y
0
Hmmmm something got lost in my posting this somehow. I'll edit it, but just in case it doesn't go through, that string should be "%i:i" nicemike40 486 — 8y
0
Wait what. Found a bug in the website, lol. Should be %i:[percent]02i" nicemike40 486 — 8y
0
Test: i %i %0i %2i nicemike40 486 — 8y
Ad
Log in to vote
3
Answered by 8 years ago
Edited 8 years ago

It is simple! You just have to use a function named string.format and some math.

Let's start with the "math" part:

local TimeLimit = 30 -- A variable to store your time! Very useful.
for i = TimeLimit, 1, -1 do -- Do this in a for loop.
    local minutes = math.floor(i/60) -- 1 minute = 60 seconds, and math.floor rounds it down, resulting in 0.
    local seconds = i%60 -- The modulus allows you to get the remainder of a devision.
end

Now that we have 0 for minutes (because it is less than a minute) and 30, 29, 28, 27... for seconds, we just need to format it to a certain pattern. We'll use string.format for this, click here for details

After looking at the wiki, let's get going:

local TimeLimit = 30 -- A variable to store your time! Very useful.
for i = TimeLimit, 1, -1 do
    for _, v in pairs(game.Players:GetPlayers()) do -- You made a typo in the question, by the way.
        local minutes = math.floor(i/60) -- 1 minute = 60 seconds, and math.floor rounds it down, resulting in 0.
        local seconds = i%60 -- The modulus allows you to get the remainder of a devision.
        v.PlayerGui.Intermission.Countdown.Text = "Intermission: " ..string.format("%d:d", minutes, seconds) -- Formats the string to M:SS.
--One more thing, I would also recommend using `string.format("d:d", minutes, seconds)` as well. It formats the string to "MM:SS". The choice is yours!
    end
end

Here you go! The string will now be formatted to "0:30", "0:29", "0:28" etc.

If you have any problems, please leave a comment below. Thank you and I hope this will help you!

0
Looks like you have the same problem I did -- the percent-sign-zero-two is removed completely for some reason. nicemike40 486 — 8y
0
ik starlebVerse 685 — 8y

Answer this question