Countdown in Milliseconds?

I’m trying to make a countdown from 5 seconds to 0 seconds in the format of (MIN:SEC:MS) but I can’t seem to get a wait fast enough to perform the calculation accurately. Here’s what I have:

while game:GetService("RunService").RenderStepped:Wait() do
	if(Miliseconds <= 0) then
		if(Seconds <= 0) then
			Minutes = Minutes - 1
			Seconds = 59
		elseif (Seconds >= 0) then
			Seconds = Seconds - 1
		end
		Miliseconds = 100
	end
	Miliseconds = Miliseconds - 1
	print(Minutes, Seconds, Miliseconds)
end

How would I be able to do this accurately?

You can’t get a function to fire 1000 times a second, but you can do this.

Instead of using a while loop, just connect the function to Heartbeat/RenderStepped.

rs.Heartbeat:Connect(function(t)
  -- lol code
end)

Doing it this way gives you t, which is the amount of time in seconds between this frame and the last frame.

Multiply this by 1000 and that’s the amount of milliseconds that have elapsed.

rs.Heartbeat:Connect(function(t)
      ms = ms - (1000 * t)
      -- other stuff
end)

It should be easy to go from there.
Remember, only Heartbeat returns t. Renderstepped does not

3 Likes

This approach makes more sense and is more accurate. Cheers!