Task.wait() wrong?

Script 1:

while task.wait(1) do

number += 4

end

Script 2:

while task.wait(1/100) do

number += 4/100

end

I want that the Number go up 4 Times every 1 sec. Script 1 works but why would Script 2 not work?

I’d recommend you just use

while task.wait(0.01) do

number += 0.04

end

It’s essentiality the same thing. Lmk if it doesn’t work.

I also did that this is not working

If you want it to go up four times every second, this would work:

while task.wait(0.25) do
    number += 1
end

You should be aware that the smallest amount of time task.wait() can wait for is 60hz (1/60) or roughly 0.01666667 (unless you have uncapped your frame rate).

Typing task.wait(0.01) will just default to task.wait(0.0166666) which is what task.wait() does without any parameters (again assuming your frame rate is 60fps)

3 Likes

Using task.wait() returns the amount of time it waited for. You can use this to do exactly what you want:

local NUMBERS_PER_SECOND = 4
local number = 0

while true do
	local timeElapsed = task.wait()
	number += timeElapsed * NUMBERS_PER_SECOND
end

Doing this would guarantee that you’re accounting for the proper time passed during the wait. With task.wait(1/100), you’re assuming that task.wait is waiting the exact same amount each time, which is not the case. You can see this with a simple test:

print(task.wait(1/100)) --> 0.02068119999603368
print(task.wait(1/100)) --> 0.01652000000467524

As you can see, there’s a difference between the two results, which wouldn’t be a huge deal if you did something like task.wait(1) because it’d be a very minor decimal error. However, since the frequency of you updating the number is less than just one second, the error occurs more frequently, causing the number variable to be less than what you were expecting since it’s waiting longer.

1 Like

Good point, always important to account for delta time.

So if its print 0.01652000000467524 would your script fill the rest numbers until 1/100?

I’m not exactly sure I understand what you’re asking, but… basically what is happening with the script if that it’ll add timeElapsed multiplied by NUMBERS_PER_SECOND to the number variable. timeElapsed is the amount of time the thread was paused for due to task.wait(). This is all happening within the while true do loop, meaning that the script will infinitely add to the number variable and never stop.

This means that if I did something like this to stop the loop:

local NUMBERS_PER_SECOND = 4
local number = 0

local loopThread = task.spawn(function()
	while true do
		local timeElapsed = task.wait()
		number += timeElapsed * NUMBERS_PER_SECOND
	end
end)

task.wait(1)
task.cancel(loopThread)
print("number:", number) --> number: 4.060616400151048

You would get the result at the last line. As you can see, after 1 second, the number variable is approximately 4.

Thanks i will try it and sometimes while true dont work for me do you know why and could the timeElapsed dont work and be bigger when its lagging to much?

If you don’t have any sort of pause/break within a while loop, there is always the potential that studio will crash or freeze momentarily until it forcefully stops the script (I haven’t done that recently, so I can’t remember exactly what happens). In other words, I don’t know why a while true do wouldn’t work for you always.

I guess there is the potential that timeElapsed is larger than normal due to some sort of “lag”, but that shouldn’t be an issue because it should still be the amount of time passed. I don’t know your use case for this loop, so I can’t say much.

@Pegagittt Reposting threads is against the forum’s terms of service, refrain from doing so.