Stopwatch to the thousandths of a second

I am currently attempting to create a stopwatch which begins from 00:00.000 and counts in the milliseconds for a player’s attempt on a “run” (attempt of a level).

The issue I have is that when I use wait(0.001) or task.wait(0.001), it is rate-limited and doesn’t tick in the correct precision.

The way I tried to get over this was by using os.clock() to calculate the difference between two times, but I want the player to be able to see the timer tick reliably.

		local playerStartTime = os.clock()
		repeat
			player:FindFirstChild("TimerMillisec").Value += 5
			if player:FindFirstChild("TimerMillisec").Value == 100 then
				player:FindFirstChild("TimerSec").Value += 1
				player:FindFirstChild("TimerMillisec").Value = 0
			end
			if player:FindFirstChild("TimerSec").Value == 60 then
				player:FindFirstChild("TimerMin").Value += 1
				player:FindFirstChild("TimerSec").Value = 0
			end
			task.wait(0.05)
		until player:FindFirstChild("TimerOngoing").Value == false
		print(os.clock() - playerStartTime)
	end

Just put it into a render stepped then add in the delta time which is given in the function

You can either switch to using render step and using the deltaTime that it provides, OR you can set the output of task.wait() to a variable.

wait() / task.wait() is assumed by most to be of return type void (i.e. it returns nothing), however it actual returns the delta time. So, if you just do

local dt = task.wait()
milliseconds += dt

then you should get closer to your desired outcome

you can use the time difference directly

local t_start = os.clock()

while true do
    local t = os.clock() - t_start
    secs = t // 1
    millisecs = t - secs
end

You mean like this that was posted 14 days ago?
(I just Searched ‘milliseconds timer’ and got a few hits)

Completely forgot I could’ve just done that, thank you so much.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.