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

Conditional statements are not functioning properly in a while loop?

While playing with loops, I've realized that some inputs for a while loop end too soon or never stop at all, even though the expression in the while loop satisfies.


local i = 0
while i~=1 do
    wait()
    print(i)
    i = i+0.01
end ``

Now, let's take a look at this loop and examine it. What it should do is increment i by 0.01, until i should equal 1. Now, of course, this loop should end, because i will become 1 at some point. But after running this program, it doesn't stop. It just continues incrementing i by 0.01. Looking at the command output, it is printing things over 1, and at one point it did print 1, but still it didn't stop.

local i = 0
while i~=1 do
	wait()
	local j = i==1
	print(i .. tostring(j))
	i = i+0.01
end``

To confirm this, I've placed variable j, storing the boolean expression i==1, and then it prints the result alongside i. From again examining this, if i is 1, then of course it is equal to 1 and j should store true, and at the end would print 1true. But again, this is not the case. Instead, it would print 1false, meaning there is something definitely wrong here.

I'm probably going mad, but if someone could confirm if this is in all versions of lua, or if it's just broken with me, please do. I'd also be happy if you could find a workaround :)

2 comments

0

Shouldn’t it be while i ~= 1 do?

0

Oops, thanks for the heads up. I forgot to update it.

1 answer

✓ Accepted answer

I'll be testing it on my end, but there are plenty of work arounds. The best one is the numeric for loop, which you can find here (Personally it's my favorite loop.)

Another way might be to change i ~= 1 to i <= 1 in your while loop.

This most likely (if there's not another problem) is due to a rounding/float issue in Lua. But yes, I highly suggest using the numeric for. It's more advantageous and it gets even more so when you learn to iterate.

4 comments

0

I have tried i <= 1 already, no luck. The idea of using a numeric for went way over my head, so I'll take this as a good answer :). I'm actually more curious on why this happens, since I've gotten weirder results after testing a bit more (i.e setting the initial i to >0.82 would actually make i resolve to true)

0

Mk. Just tested the same loop on a different language in a different compiler. Same results. It's a rounding error then, and unfortunately I can't do anything about it. Thanks anyway.

0

Well actually, there is a way to get around the rounding error. (If it that.)

0

Well actually, there is a way to get around the rounding error. (If it is that.) but you would lose some of the data in the process. Basically, you take 10 to the X power, (X is your accuracy), then multiply that number and your number. After this, you provide as an argument for math.floor() and then divide the number it gives you by 10^X. It's basically "rounding" a number.

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