"for" loop delay

I’m working with a "for i = " loop, and I’m getting some weird results out of it.

This is my script:

local START = tick()

for i = 1, 100 do
	
	wait(0.1)
end

print(tick() - START) -- Should be 10 seconds

Though at the end, it prints this:
image

Does anyone else have this issue?

2 Likes

do

print(wait(.1))

And you’ll see the actual time iyt waited for.

Lua is single threaded, Meaning it has to go though everything and then go to yours. Like a list

Your Loop
Example Thing
Example Thing
Example Thing

Loop again.

Those Example Things is that wait difference

1 Like

So it is not really possible to have a close to accurate 10 seconds loop like this? I think I used this method before, but never had those results.

Maybe you should try pausing the loop? Here

wait doesn’t yield for an exact amount of time. When you use weight the program will basically take a break and then come back when it gets around to it after the time has expired. You are stacking the inaccuracies with a loop but if you just did a single wait(10) it would be reasonably close to 10. If you really wanted to loop in this sort of fashion though you could also do this.

repeat wait(.1) until tick()-start>10

Well my main goal is to lerp cframe a part using an easing module, this is my script for that:

local EaseModule = require(game.ReplicatedStorage.ease)

local cf = workspace.AAAA.AB.CFrame

local RunTime = 1/24

local Seconds = 10

local End = Seconds / RunTime
--
wait(5)

local Record = tick()


for i = 1, End do
	local lerpValue = EaseModule.outSine(i, 0, 1, End)

	script.Parent.AB.CFrame = cf:Lerp(script.Parent.B.CFrame, lerpValue)
	
	--
	wait(RunTime)
end


print(tick() - Record)

And I want it to be exactly 10 seconds. I know I could use tweenservice, but that’s not what I want to achieve.

Tweenservice on the other hand takes exactly the time you want, and I was hoping this worked the same.

But “repeat wait(.1) until tick()-start>10” would be a “solution” for this?

1 Like

If you dont actually need to wait(0.1) - say you are making a rendering engine with frames the industry standard is to create whats called a double buffer. assert each frame into a 2d matrix of colors then have the for loop update the colors with your calculation function, then update them with that data. Like in opengl that’d be like

 glColor(1.0, 0.0, 0.0, 1.0); --this assigns what the color for all our indices in our color buffer will be
glClear(GL_COLOR_BUFFER_BIT) --actual for loop those values into the color buffer

Yeah that’s not what I want, read the post above it

have you considered subtracting the wait time by how long it took to process the last for loop step?

Wait returns the exact amount of time that it took. I’ll give you an example in a second, but if you’re lerping that is the solution.

Edit: dthecoolest has the solution

1 Like

The standard is to use delta time to account for the inaccuracies in wait(), try out this formula.

Also for @CoderHusk opengl and calculus is not always needed.

2 Likes

It was just an analogy, no calculus was in it. I just wanted to suggest it cause its a fairly simple concept to grasp and helps with a lot of problems involving large data samples

1 Like