I’m interested in making a timer that counts down. Usually when I make a timer for something like a round-script, it just looks like:
for i = 5,0,-1 do
script.Parent.Text = "Intermission: " .. i
end
Something like that, but I’m trying to make a clock style timer. So instead of saying 60 seconds it would say 1:00… The thing is, I have no idea where to start. I’ve searched up tutorials and looked for similar posts, but couldn’t find anything that could help me. If you have any ideas how I could go about doing this let me know!
Never mind I found a solution. For anyone who sees this post in the future. Here is my script:
for i = 15, 0,-1 do
local minute = tostring(math.floor(i/60))
local sec = math.floor(i%60)
if sec < 10 then
sec = "0".. tostring(math.floor(i%60))
end
status("Intermission: " .. minute .. ":" .. sec)
wait(1)
end
Just saying; there’s a better way to handle your seconds. I want to introduce you to a cool string pattern that I learned the other day, %.2i. This pattern will insert a 0 as the first number if the number is below 9. Other ways to write this are %.2d, %02d and %02i. You can also replace 2 with 3 so it’s under 99 for a 0 to be added, so on.
Here’s some code that will automatically insert that awesome 0 for you. Before I post that, I’d like to point out that your concatenation could’ve used the calculated sec value instead of doing it again.
for i = 15, 0, -1 do
local minutes = math.floor(i/60)
local seconds = math.floor(i%60)
status(string.format("Intermission: %i:%.2i", minutes, seconds))
wait(1)
end
The response I created here was based off of what OP originally resolved their question with which involved a status function of some kind. My code was only intended to improve on theirs. Presumably status is a function that’s meant to update a value of some kind somewhere.
You can write your own status code. The main idea, if you’re looking to my post for formatting numbers, should be everything except the status function so that means the string formatting and calculations done to retrieve the time. It is a pretty simple piece of code but a little old and missing some parts like some subtractions but it still gets the job done.
@AC_Starmarine It’s only a bump if the post doesn’t contribute to the thread. Asking a question is fine, leave it be. Flag a post if you believe it doesn’t belong rather than calling someone out for it.
Alright! Thank you. I was just scrolling through things to see if I can get an Idea of a code that will fix my problem as well with the format of the Numbers (0:0).