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
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