Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 5 years ago
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
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. Cousin_Potato 129 — 5y
0
Should've posted that as an answer m8 RayCurse 1518 — 5y

3 answers

Log in to vote
0
Answered by 5 years ago
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.

Ad
Log in to vote
-1
Answered by 5 years ago
Edited 5 years ago
  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
0
the only way to fix this is when the world gets better computers to run the job faster TheluaBanana 946 — 5y
0
OMGGGGGGGGGGG XDDDDDDDDDDDDDDDDDDD LOLLLLLLLLLLLLLL this is legit the funniest thing EVER!!!! greatneil80 2647 — 5y
Log in to vote
-2
Answered by 5 years ago

There is no solution.

0
Another meme lol. Introducing the "U simply can't." v2.0 RayCurse 1518 — 5y
0
Rip alpha and 4 rep xd greatneil80 2647 — 5y
0
bad hoot hoot TheeDeathCaster 2368 — 5y
0
Yes there is, using a longer wait time. Also refrain from posting what should be comments in the answers. User#19524 175 — 5y

Answer this question