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