Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Why does my for loop reach the end in 4 seconds?

for i = 1,100 do
	print(i)
	wait(1/100)
end

This for loop was meant to reach a 100 in one second because it runs a 100 times and waits 1/100th of a second (0.01) each and 100*0.01 is 1 second. But the code actually reaches a 100 in 4 seconds for some reason when I stopwatch it. Can anybody explain why and also a solution?

2 comments

2

The minimum wait time is 29 milliseconds. There is no guarantee the thread will wait exactly 29ms. 'print()' takes time too. There is no guarantee each loop will execute in a consistently timed manner.

0

Should've posted that as an answer m8

3 answers

for i = 1,100 do
	print(i)
	wait(0.01) -- Not the same as 1/100 (in strict Lua terms, but this is what you should be aiming for)
end

You're welcome.

  1. your stopwatch is broken
  2. your game is broken 3.your stopwatch and game are broken 4.all of the above 5.(the only semi-legit thing im saying)assuming your testing this in studio, thingy here needs time to process some things first before it can move on to run the code (e.g the print()), especially when the wait() thing is more than 2 decimal places, it becomes clear that its not accurate and u might wanna tune the number down a bit

2 comments

0

the only way to fix this is when the world gets better computers to run the job faster

0

OMGGGGGGGGGGG XDDDDDDDDDDDDDDDDDDD LOLLLLLLLLLLLLLL this is legit the funniest thing EVER!!!!

There is no solution.

4 comments

0

Another meme lol. Introducing the "U simply can't." v2.0

0

Rip alpha and 4 rep xd

0

bad hoot hoot

0

Yes there is, using a longer wait time. Also refrain from posting what should be comments in the answers.

This discussion is preserved as it was. New answers, comments, and votes are closed.