The for loop does not seem to work properly

Hello!

I want to make a cframe lerp door system that uses the for loop. However, there is a problem when the third argument for the for loop is smaller than 0.1.

Example 1: for i = 0, 1, 0.1 do print(i) end, last print is 1

Example 2: for i = 0, 1, 0.01 do print(i) end, last print is 0.99

Why does example 2 not print 1?

I do not know why it’s not doing it correctly, maybe it’s to do with low decimals :thinking:
Anyways to solve your problem you could still use example 1 but divide i by 10, example:

for i = 0, 1, 0.1 do 
print(i/10) 
end
1 Like

This is a kind of duct-tape solution, plus your example will only count to 0.1 but that can be fixed with some repeating and math. Anyways, thanks.

Why don’t you just use TweenService? It’s way easier to do interpolations that way

2 Likes

If you want to lerp a cframe, Vector3 has a lerp function.

Vector3 Vector3:Lerp ( Vector3 goal, number alpha )

alpha is the fraction by which to interpolate the Vector3 to the goal.

I heard tween service was inefficient (especially for anything is not vector2 or udim2)

but idk if that’s true or not just heard it

The last number is actually .99 + 6.6613381477509e-16, so adding .01 would bring it over 1. Floating-point precision is the cause of the problem here. You can get around it by looping over the integers from 0 to 100 and dividing by 100.

5 Likes