Value conversion to (0:00.0) not working as intended?

Right now Im trying to make a stopwatch thing that includes milliseconds which is where I have a problem. The milliseconds is not working the way I want it to and I can’t think of the solution.

this is what I want it to do
0:00.1
0:00.2
0:00.3
0:00.4
0:00.5
0:00.6
0:00.7
0:00.8
0:00.9
0:01.0

but right now its doing
0:01.1
0:02.2
0.03.3
0:04.4
0:05.5 etc.

This is my script right now

local function ToMS(s)
	return ("%2i:%02i.%i"):format(s/60%60,s%60,s%10)
	
end

for i = 1,270 do
	print(ToMS(i))
	wait(0.1)
end

Some help would be appreciated :slight_smile:

I’ve found so much trouble trying to explain why this isn’t working and it’s because the way each item is interacting with each other is fundamentally weird. I can’t find the right words to articulate it. Took me a solid few minutes of testing and rewriting to figure out how to say this and it’s still weird.

You’re counting 270 seconds across 27 seconds. The for loop counts integers from 1 to 270, there’s no room for partial seconds but you make up for that by changing the wait time instead. But then your ToMS function treats the input as seconds. The milliseconds format is actually just the same thing as the seconds one but instead of resetting at 60, it resets at 10 - these aren’t actual milliseconds.

Instead of trying to figure out this weird math, I just wrote a new one that works based off of Heartbeat frequency. Much easier to do and it provides more accurate timing than wait. Heartbeat provides partial seconds as the measure is the time between two frames executing.

local RunService = game:GetService("RunService")
local Heartbeat = RunService.Heartbeat

local FORMAT = "%d:%.2d.%d"

local function getTimeFormat(aggregate)
	local milliseconds = math.floor(aggregate*10)%10
	local seconds = math.floor(aggregate)
	local minutes = math.floor(seconds/60)
	
	print(FORMAT:format(minutes, seconds, milliseconds))
end

do
	local agg = 0
	for i = 0, 1000 do
		agg = agg + Heartbeat:Wait()
		getTimeFormat(agg)
	end
end

For loop is only for testing, I hope you don’t actually use that in production code. Time should be held in a different way if at all possible, such as a while loop counting down an arbitrary timer.