Timer isn't accurate

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()	

2 Likes

0.001 is a millisecond. 0.01 is 10 milliseconds.

Ok? Wdym? Even if I change it to 0.001 the problem still exists

Hmm :thinking:

Is this on client? Maybe doing run service render stepped?

Yes, it’s on the client. I dont see how putting this on RenderStepped would help, as this loop still needs to run every 0.01 second, not every frame

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.

2 Likes

Hmm :thinking:

Try this

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()

If not, ill just wait til I get home and check.

2 Likes

So task.wait is basiclly within frame rate?

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.

2 Likes

Hmmm. Ok :ok_hand:

I guess i learnt something today! Thanks for sharing i wad also lost!

It looks good, but I don’t know how to tell. It feels slightly faster than a second

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

What? This just constantly sets the label’s text to time(), which is apparently 1.2

i have edited the code, try now, i did a mistake in it.

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?

For A More accurate one may be do wait(0.000001) instead of wait(0.01), put a timer beside your pc screen and check, although its correct.

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)
3 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.