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?
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
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!