Need someone to explain what string.format does?

Hello, I was wondering how string.format() works.

I am wondering this because I want to make a timer with minutes and seconds and stuff and I think it has something to do with string.format().

If you could help it would be much appreciated! :slightly_smiling_face:

It allows you to format strings using certain format specifiers. For example

local name = "incapaz"
print(string.format("hello, %s", name))

This will print "hello incapaz" since "%s" specifies what will be “plugged in” should be a string. So the "%s" essentially gets replaced with name.

For your case, you’ll want a format specifier of "%i:%02i", "%i" means integer, the : is there to obviously separate the minutes from the seconds, and the 02 is just padding a leading zero to the left if the seconds is only 1 digit long. So 65 seconds would be represented as 1:05 and not 1:5.

for i = 60, 1, -1 do
    print(string.format("%i:%02i", math.floor(i/60), i%60))
    wait(1)
end

print("clock finished!")
5 Likes