Problem with millisecond timer / counting time 'while true do' loop in a script

Hello everyone, I have a problem with my millisecond timer. I don’t know what exactly is the problem but that’s a script I use and as result something doesn’t work correctly. After in game round repeats timer to count from 0 for several times, timer starts to count time slower and slower every round. I’d like to achieve a normal millisecond timer that doesn’t change any speed of counting while looping a server script.

So this is my ‘timer’ script:

local status = game.ReplicatedStorage:WaitForChild("Status")
local ingame = game.ReplicatedStorage.Ingameplayers

while true do

repeat wait() until status.Value == 'GO!' 

local Minutes = 0 
local Seconds = 0 
local Milliseconds = 0

while true do
repeat
local passed = wait() 
Milliseconds = Milliseconds + math.ceil(passed) 
status.Value = (Minutes..":"..Seconds..":"..Milliseconds) 
until Milliseconds > .99 
Seconds = Seconds + 1 
status.Value = (Minutes..":"..Seconds..":"..Milliseconds) 
if Seconds == 60 then 
Minutes = Minutes + 1 
Seconds = 0 
status.Value = (Minutes..":"..Seconds..":"..Milliseconds) 
end
if Minutes == 60 then 
Minutes = 0 
status.Value = (Minutes..":"..Seconds..":"..Milliseconds)
end
 if ingame.Value == 0 then
	status.Value = '50:0:3000'
end
 if status.Value == '50:0:3000' then
	wait(0.2)
	status.Value = 'Out of time!'
	break
 end

  end
end

I don’t get any error or anything. I had thoughts that maybe a while true do makes some messed up here but I am not really sure. I tried to remove the first while true do but script doesn’t repeat every round so game brokes and status value is set to ‘GO!’ and nothing changes. I also tried to call it with RemoteEvents but I can’t because I have both written in a script. I don’t really understand why this happens so if anyone understands, please feel free to reply to this topic. If you have any questions about script let me know. :herb:

In general I’ve heard that using repeat wait() until is a bad idea, take a look at this thread for instance. Instead of using repeat wait for the value of status changing, maybe use a bindable event to trigger the timer. If it is counting up couldn’t you also just have a starting point and use something like tick() to get the time elapsed since and format it with os.date? This could possibly help a little bit, not sure.

3 Likes

This is why you should use tick() to do the work for you, it includes milliseconds. I lost the function somewhere in my scripts, give me a moment to retrieve it. This post will be updated soon™.

Dear ArticGamerTV

Wait is a really bad idea if you are counting milliseconds. Also you are doing a while wait() do loop inside of a while true do. Even so the while true do is triggering the other loop as fast as it can. If you were doing this on a server then man oh geez you’re in for a rough time. Even on client unless you have a super-0fast supercomputer server farm (bit of an exaggeration) I don’t think you could run this. “wait() fits between 0 and 30 times inside wait(1), the exact amount of times is undefined”, (buildthomas [Community Sage] link). I would also refer to this for more info. To wait a millisecond it would be wait(0.001). I would only really go as far as wait(0.05) for most things though. I hope this helped.

Good luck, -HiddenKaiser

1 Like