How can i make milliseconds count down faster than seconds?

I’m making a countdown timer, and I can’t get the milliseconds to count down faster than the seconds

This is what im trying to go for

I have a working script that already counts down, but I’m just wondering if the milliseconds can count down as fast as the right 2 numbers in the video

This is the script:

local label = script.Parent
local Hours = 0
local Minutes = 11
local Seconds = 0
local Milliseconds = 0


while true do 
	if Milliseconds > 9 then
		if Seconds > 9 then
			if Minutes > 9 then
				if Hours > 9 then
					label.Text = Hours .. ":" .. Minutes .. ":" .. Seconds .. ":" .. Milliseconds
				else
					label.Text = "0" .. Hours .. ":" .. Minutes .. ":" .. Seconds .. ":" .. Milliseconds					
				end
			end
		end
		
	else
		if Seconds > 9 then
			if Minutes > 9 then
				if Hours > 9 then
					label.Text = Hours .. ":0" .. Minutes .. ":0" .. Seconds .. ":0" .. Milliseconds
				else
					label.Text = "0" .. Hours .. ":0" .. Minutes .. ":0" .. Seconds .. ":0" .. Milliseconds						
				end
			end
		end
		
	end

	if Milliseconds > 0 then
		Milliseconds = Milliseconds - (1 / 100) * 1000
	else
		if Seconds > 0 then
			Seconds = Seconds - 1
			Milliseconds = 59
		else
			if Minutes > 0 then
				Minutes = Minutes - 1
				Seconds = 59
			else
				if Hours > 0 then
					Hours = Hours - 1
					Minutes = 59
				else
					break
				end
			end
		end
	end
	wait(1)
end
1 Like

Your wait(1) is waiting 1 second. So the countdown is only updating every second.
And you may need to hook up to the RenderStep, I think that while true loop with no to little waiting may hit execution limits.

1 Like

Do you have any idea how I could do that? The runservice documentation was a little confusing

1 Like

use task.wait() in the while loop to update every frame, subtract the delta time from the timer, and when displaying the time, round it to the nearest hundredth

1 Like
local function onRenderStep(deltaTime)
	-- code to update the label here.
end

-- This is just a visual effect, so use the "Last" priority
RunService:BindToRenderStep("TimerCountdown", Enum.RenderPriority.Last.Value, onRenderStep)

And then unbinding whenever you are not showing the countdown so it doesn’t keep running
RunService:UnbindFromRenderStep("TimerCountdown")

Also in general that looks like a lot of work you’re doing keeping track of the seconds/minuts/hours there. I would really consider just having something like:

local EndTime = tick() + (11 * 60) -- for 11 minutes 

local function onRenderStep(deltaTime)
		local now = tick()
			
		local minutes = math.floor((EndTime - now) / 60)
		local seconds = (EndTime - (minutes * 60)) - now
		local format = "%02d:%02d"
			
		label.Text = format:format(minutes, seconds)
end
1 Like

thank you! How can i add the hours and milliseconds? I want it to show in the timer. And how could i get the timer to show in the client? When i test in studio, it only shows on the server side

1 Like

Type stopwatch in the SEARCH up top.
The problem with task.wait is it’s unreliable.
You should be using a reference to actual operating system time to get an correct number.

1 Like

I did a little tinkering so now its to my liking, but I still cant get it to show when im testing in client mode. Any help?

1 Like

In client mode? Do you have this in a Local script that’s updating the GUI element and is the GUI set to be visible?

1 Like

I have it in a regular script and the GUI is set to visible

1 Like

There is a significantly more convenient method to displaying an amount of seconds in a digital clock format. It is achieved through the use of string patterns and math:

local RunService = game:GetService("RunService")


local RENDER_FORMAT = "%02d:%02d:%05.2f"



local function renderCountdown(textLabel: TextLabel, totalSeconds: number): RBXScriptConnection
	local function render(hours, minutes, seconds)
		textLabel.Text = string.format(
			RENDER_FORMAT, 
			hours,
			minutes, 
			seconds
		)
	end
	
	local connection
	
	connection = RunService.PostSimulation:Connect(function(deltaTime: number)
		if totalSeconds <= 0 then
			connection:Disconnect()
			
			render(0, 0, 0)
			
			return
		end

		local hours   = totalSeconds // 3600
		local minutes = totalSeconds % 3600 // 60
		local seconds = totalSeconds % 60

		render(hours, minutes, seconds)

		totalSeconds -= deltaTime
	end)

	return connection
end

It is even more convenient to represent your clock as HH:MM:SS.MS

https://gyazo.com/6c9d42426a1fa136839a6415f600735d

1 Like

You would most likely need it to be in a LocalScript.

1 Like

Oh, is there any way to make it work in a regular script or would i have to make a different script?

“Script” scripts are server scripts that only run on the server.
“LocalScripts” scripts are client side scripts that only run on the individual players client.

For pretty much any scripts that are updating a GUI you would need it to be a “LocalScript” object instead of a “Script” object.