Task.wait(1) waits less than 1 second?

for some reason task.wait(1) waits less than a second, or time() is weird
I’m trying to check if time() - lastTimeRn >= 3.9 which in theory should work
this is my script

	lastTimeRan = time()
	
	task.wait(1)
	print(time() - lastTimeRan)
	task.wait(1)
	print(time() - lastTimeRan)
	task.wait(1)
	print(time() - lastTimeRan)
	task.wait(1)
	print(time() - lastTimeRan)
	if time() - lastTimeRan >= 3.9 then
		print("yes3")
	end
		

but instead i usually get this in the output:
image
and so the conditional doesn’t happen
I have no clue why this is happening

Maybe because of an scripting error? I think you need to check for scripting errors in the script.

There aren’t any errors showing up, and this is the only part in the script that ever defines lastTimeRan

task.wait() will never always yield exactly one second, its always more or less.

1 Like

Oh. Sorry about that. My mistake, lol.
If you look, I am not a professional at scripting. My guess is just to perfectly re-do it or maybe get help from a scripting supporter.

It won’t yield exactly 1 second, but that is way too far off to be normal. What is your frame rate?

try

local totalTime = 0
for i = 1, 4 do
	totalTime += task.wait(1)
	print("Iteration", i..":", totalTime)
end

image

You’re just timing it wrong. Is something interfering with lastTimeRan?

1 Like

I think its time being weird try tick() or os.clock(), not os.time() as it only measures full seconds.

I noticed that the script actually works as intended after about 10 seconds of the server/game existing.
there is nothing interfering with lastTimeRan other than that

1 Like

Is the code running multiple times, at the same time?
If so, it’ll set the variable, and start waiting
If it runs again and the variable is set, when the first wait finishes, it’ll use the new variable.

os.clock() seems to actually work regardless of when the server started, thanks

Nope it does not run multiple times

Great,

Seems like from the testing that time() uses game time which is susceptible to lag or something happening when starting the game.

Os.clock is cpu time so it uses real time.

Idk thats my best guess.

1 Like
  1. Task.wait isn’t accurate 2. You’re timing it wrong 3. Performance affects the time.
1 Like

You don’t want to use os.clock in actual production. It’s made for debugging/benchmarking purposes only and accurate in studio and not client.

What if the code is on the server?

Just do remote events or use an alternative lol

How would I actually time it correctly if not this?

os.clock is accurate, both in Studio and in-game. It measures CPU time, intended for benchmarking, and will work both in Studio and in-game (client and server). It should be noted that there will be a lag spike when a server starts, as well as on the client when the player first joins, and potentially with any future lag spikes due to poor performance.

task.wait is accurate at least to the first decimal (10th of a second), and will only be seen at most 1.01 seconds in normal conditions. The accuracy will vary with things such as performance and frames. Heartbeat is calculated at the FPS level, after physics calculations (Stepped occurs before). The higher the framerate, the better the accuracy.

In short, task.wait is accurate enough for almost every scenario.

Also, even with lag spikes, task.wait does appear to remain accurate for the most part (especially if the waiting period is longer).

1 Like