I’ve made this timer using this script, and it’s actually taking 1.6s for every 1s on the timer. I assume it may be due to the time it takes for the Label’s text to actually be changed etc. Any ways I can get around this?
function StartTime(StartingTime)
if not StartingTime or StartingTime == 0 then
local t = 0
local tickety = tick()
while true do
if math.round(t) == t then
print(tick() - tickety)
tickety = tick()
end
t += 0.01
t = round(t)
Label.Text = tostring(t)
task.wait(0.01)
end
end
end
StartTime()
Task.wait() yields code execution till the next step. The standard rate for this is c. 60fps, or 0.016ms, any value lower than this will simply just run on the next step, and this seems to correspond with the printed values.
function StartTime(StartingTime)
if not StartingTime or StartingTime == 0 then
local startTime = tick()
local label = script.Parent.Label -- Replace with your actual Label reference
local updateInterval = 0.01 -- Update the Label every 0.01 seconds
while true do
local currentTime = tick() - startTime
label.Text = string.format("%.2f", currentTime)
wait(updateInterval)
end
end
end
StartTime()
It schedules code to be resumed on the step following the elapsed time. I haven’t looked into it in detail, but it generally means using task.wait() for accurate timers is not reliable.
I have seen a method to get the alpha time (which is a measurement of time between steps) which might be better, but not sure on implementing it.
Why are you are using a t variable to store time and increment it, instead why aren’t you just using time() function instead, like this:
function StartTime(StartingTime)
if not StartingTime or StartingTime == 0 then
local outStartTime = time()
while true do
local inStartTime = outStartTime - time()
--My Way To Round Without Removing the hundred-ths decimal unit
inStartTime *= 100
inStartTime = math.round(inStartTime)
inStartTime /= 100
--My Way End
Label.Text = inStartTime
end
end
end
I’ve added a necessary task.wait(0.1), since execution time was exhausted. It looks good, but I don’t know how to tell. This is pretty much the same as @Marcosygian’s response but with time() instead of tick(). Is there a difference between time() and tick()? And how should I tell if this is working and it’s accurately a second?
I had another look at it and wrote this simple thing, see if this works better.
--LocalScript
local rs = game:GetService("RunService")
local label = script.Parent.Frame.TextLabel
local runConn
local function startTimer()
local elapsedTime = 0
runConn = rs.RenderStepped:Connect(function(delta)
elapsedTime += delta
local smallTime = math.round(elapsedTime * 100) --added to add the 100ths of a second
label.Text = smallTime / 100
end)
end
local function endTimer()
runConn:Disconnect()
runConn = nil
end
task.delay(5, startTimer)
task.delay(15, endTimer)