Timer that runs every 0.01 seconds, slower in studio but faster in game?

So I have a timer in my game and I tested going pretty much exactly the same speed until I touch the part that stops the timer and on studio the time was more but when I tested in game going the same exact speed it was like almost 5 seconds less?

The timer is on the server and I’m firing it every 0.01 seconds to all clients, also testing both times I was at the same starting position

while self.timer do
	self.elapsedTime += 0.01
	RemoteManager.Timer:FireAllClients(self.elapsedTime)
	
	if self.elapsedTime > self.maxEventTime then
		RemoteManager.TimerEnd:FireAllClients()
		self:RemoveCurrentHeat()
		break
	end
	
	task.wait(0.01)
end
3 Likes

Task.wait is dependent on fps and because Roblox studio uses your own PC as the server hoster your fps might be lower or higher than a Roblox-hosted server this might be your problem

4 Likes

So in that case what should I do because I already tried telling the clients to do their own loops while I calculate the elapsed time on the server and when it reaches a certain time tell all the clients to end their loops but, the client ones were out of sync

1 Like

Why do you need this loop and timer exactly?

1 Like

I mean most games have loops and timers, but answering your question i’m making a track game and I need a timer for it that im displaying on each players screen ( basically)

1 Like

Most games have loops and timers but they don’t need to be exact and ping would be an issue does the time that you need to track has any decimals

1 Like

well yeah so since its a track game i need it to track to the two decimal places, for just accuracy also thats how it is in real life track and field

@ali432567 So what do you think I should do?

Heres just a mini video of the timer currently

I have an idea give me a second I need to code it

1 Like

alright i fixed it, so instead of just updating the loop by 0.01 seconds, which can cause discrpancies in timing because of stuff like proccessing power, network latency and frame rate

I used a more accurate method to calculate the elapsed time which doesnt rely on a fixed increment but instead calculates the difference in time between each iteration using os.clock

while self.timer do
	local currentTime = os.clock()
	local deltaTime = currentTime - prevTime
	self.elapsedTime += deltaTime
	RemoteManager.Timer:FireAllClients(self.elapsedTime)
	
	if self.elapsedTime > self.maxEventTime then
		RemoteManager.TimerEnd:FireAllClients()
		self:RemoveCurrentHeat()
		break
	end
	
	prevTime = currentTime
	task.wait(0.01)
end

@ali432567 Thank you for taking the time to reply, means alot!

3 Likes

can’t you just use wait(0.01) instead? I don’t get the difference

wait is less accurate and slower

1 Like

uh i think wait is actually deprecated

1 Like

oh yeah it is, gotta just update all my code now, thanks for the heads up

1 Like

Doing it this way actually adds 0.01 seconds extra each loop use Heartbeat’s delta time and filter the first 4 digits including dots

1 Like

Wait so is that bad or should I change the code?

2 Likes

Change the loop to a HeartBeat connection it returns an argument called DeltaTime it is more accurate than os.clock()

2 Likes

Nice to see you already got the answer.

The other issue is that since the way task.wait() is done the minimum time you can get with just task.wait is just .03 seconds, so using (.01) isn’t going to go any faster than (.03).

2 Likes

task.wait’s minimum wait time is actually basically the same as waiting a frame(so with 60fps it is 0.015~ seconds)

Oh wait, maybe I’d heard that wait() was .03 seconds, not task.wait…

2 Likes

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