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

I don't understand this?

I'm kind of a noob to some things in lua, and one thing I cannot seem grasp is pairs and ipairs.

Can someone, in simple terms, explain to me why for x = 1,5,2 do print(x) end prints 1, 3, and 5?

1 answer

✓ Accepted answer

For loops have a variable, in your case, x. If I do this code,

for x = 1,5 do
	print(x)
end

It will print 1 through 5. This is because each time the for loop gets to "end", it adds 1 to x. X starts off at 1, but then each time the computer sees "end", it goes back up to the top of the loop and adds 1 to x. Therefore, the first time this code runs x = 1. Then, x = 2. Then 3, and 4, and 5. Once it reaches 5, it stops.

But there's another, optional number that you can put it. By default, the loop adds 1 to x every time. But if you put in the third number, it will take that number and add it to x instead. Therefore,

for x = 1,5,2 do
 	print(x) 
end

will add 2 to x every time it reaches "end".

1 comment

0

Ok thank you that's what I needed.

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