A few questions about a timer script

I retrived this code from a post about a timer script and was wondering what the milliseconds variable means?

local milliseconds = ("%.3s"):format(tostring((diff % 1) * 1000))

“diff” was a parameter for the function

That line of coding is getting milliseconds from tick because os.time doesn’t support milliseconds. When you do something like print(tick()), you will see a big number with a bunch of decimal places, which is actually because of how roblox truncates printed numbers unless otherwise formatted.

tostring((diff % 1) * 1000)

This part is simply getting the milliseconds from tick (diff). But this number will have many decimal places, and you really don’t need that level of precision.


This part is formatting the milliseconds to only display the first three digits. % is a lua string directive, and f (floating-point number) is an instruction on how the string should be formatted. Everything between that is also instructions to format the string. The .3 is used to round the formatted number to the nearest 3 decimal places.

For example, if diff was 123456.7890123, then tostring((diff % 1) * 1000) would be 0.7890123, and ("%.3f"):format(tostring((diff % 1) * 1000)) would be .789

1 Like

Thanks man! Also, why is multiplying it by a 1000 needed?

I didn’t know why, so I tested it real quick with this code:

local diff = tick()
local x = ("%.3f"):format(tostring((diff % 1)))
local y = ("%.3f"):format(tostring((diff % 1) * 1000))
print(x) >> 0.133
print(y) >> 132.678

It appears I was slightly wrong with my explanation, the "%.3f" string rounds the first three digits, not decimal places. diff % 1 will return the decimal places of diff, and lua will format the result as 0.nnn, in which the 0. is unneeded. Multiplying that by 1000 will remove the 0. automatically added in lua number formatting, so you will only get the three digits you need to then manually format into the final string to display time.

1 Like

You’ve helped me alot already and I thank you for that! I have just one more question. Can you explain what subtracting two ticks together do?

local t = tick()
displayTime(tick() - t)

tick() returns the time in seconds since the January 1st, 1970. This is called Unix Time, and it is what computers consider the “start of time”. Subtracting two ticks is just the same as subtracting any two numbers, but since it stands for a unit of time you will get time elapsed in seconds.

For example, wait() yields the script for about 0.33 seconds. You can easily test this by subtracting ticks.

local avg = 0
local rep = 20
for i = 1, rep do
	local start = tick()
	avg = avg + (tick() - start)/rep
print(avg) >> 0.033377945423126 -- wait() is about 0.333 seconds

tick() can often be used for testing purposes, such as measuring the delay between the client and the server.

local start = tick()
RemoteFunction:InvokeClient(player) -- yields until client responds to server
print(tick() - start) >> 0.46869516372681 -- about a half second delay
1 Like