[Help]I'm stuck...

So, I’m a server owner and I thought it was about time to start looking into Lua for my server.
anyways I’ve been looking at normal Lua and I’m stuck on Example 20(while statement) of the QuickLuaTour that you get from lua for windows.
and I don’t want to just keep going and forget it because that just wouldn’t work :expressionless:


x=0
while x~=9 do 
	x=x+1
	io.write(x.." ")
	print()
end

I get, when 0 isn’t equal to 9 do whatever but I don’t get x=x+1 i also don’t get io.write(x…" ") when it could just be io.write(x) but I con’t care about that.
Also i just assumed this is where to post this. I was split on this and, Help & Support.-Vin

x=x+1 increments x by 1

…" " adds a space, so it wouldn’t be 1234567… it would be 1 2 3 4 5 6 7

So it sets x to 0
is x~=9? yes ok do this
0 = 0 + 1
write "1 "
print
is 1~=9? yes ok do this
1 = 1 + 1
write "2 "
print

etc.

The way loops work, is that they keep repeating until a condition is met. In the example what it is saying is:

While x is not 9 then do this.

So the first time it loops it goes in order.

x = x+1 means make the value of x equal to the value of x + 1. The reason is so we can eventually break the while loop. Because if you think about it, x will always never = 9 if you don’t make it 9. So without it it would loop infinitely. Each time it loops x will have a new value until it reaches 9 which is when the loop is told to stop.
First loop would be:

x(which is 0) = x(which is still 0) + 1

Which if you can do math is 1
So it now becomes x = 1
Then the next time it would be

x(which is 1 because it changed the first time it looped) = x(which is still 1) + 1
Which makes x = 2. You get the idea, eventually it x = 9 and then the condition for the loop will be false (since if 9 is not 9, which it is)

For the io.write, its basically saying output x each time but with a concatenated string, which basically conjoins the two together. So it would be: x<space> since " " is a space.

So in the end you’ll end up with

1 2 3 4 5 6 7 8 9

and it will end at 9 because the statement is false, because 9 does = 9.

I probably waffled a bit but I tried to explain it asthough I was talking to you in person. Hope this helps.

Alright, Got it thanks! :slight_smile: