Hello! I am working on a game and want to use a timer that uses minutes instead of having the seconds only. I have been trying for hours to do this but I cannot get it down. Thanks for the help!
I’ve actually created a module to allow you to convert a number of seconds to a timer format. Read more here:
I recommend you use this because normally, you’d have to have many for loops within each other, and it just gets crazy. So, all you have to do with the module is iterate over the number of seconds and convert them each time with the module.
local function digital_format(n)
return string.format("%d:%02d", math.floor(n/60), n%60)
end
The "%d"
is a format specifier for digit, the "%02d"
specifies at least two digits and zero-padding. So if the number were 9 it would be 09
in the string. If it were 10 it would just be 10
.
Woah!! How does this even work?
He’s utilizing string patterns. %d
refers to digit. He’s inputting two digits:
- The number of minutes in the seconds (seconds / 60)
- The number of seconds remaining after factoring out minutes in #1 (known as modulo)
The second number is given a padding of 2 zeros max
That’s actually really helpful. I’ve haven’t used or heard of this until now! Thanks for the link since I’ll be learning about this for the next 1-2 week.
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.