Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

What does the common, "for i = 1, #Table" for loop mean?

Asked by
Zeuxulaz 148
4 years ago

What does it mean?

I know exactly what it does but I have no idea what it actually means; what does i mean? Does it stand for something?

Example:

01local Value = 0
02local WorkspaceChildren = workspace:GetChildren()
03 
04for i = 1, #WorkspaceChildren do
05 
06    local Child = WorkspaceChildren[i]
07    Value = Value + 1
08 
09    Child.Name = Value
10 
11end

The code sets all the children of workspace to a number (which goes up in 1 every child).

0
? Zeuxulaz 148 — 4y

2 answers

Log in to vote
1
Answered by 4 years ago

The for loop has a beginning and an end number. For example (no pun intended)

1for i = 1, 10 do
2    print(i)
3end -- Code made by Qub_lex

It would loop through all numbers from 1 to 10 and print all of them.

The # means length or how many items there are in a table. For example:

1local Fruits = {"Apple", "Banana", "Orange"}
2print(#Fruits)

Would return 3, because there are three items inside the table.

Edit: Just realised I didn't even answer your question. i is a placeholder variable that stands for "Integer" or sometimes "Index".

0
Thanks! You made it very clear. Zeuxulaz 148 — 4y
Ad
Log in to vote
1
Answered by
sngnn 274 Moderation Voter
4 years ago

i would be the current number the loop is at.

If you ran this in the command bar:

1for i = 1,10 do print(i) end

The output would be counting up, as it keeps printing the current number (i) as the loop continued.

0
Thanks for the explanation! Zeuxulaz 148 — 4y

Answer this question