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
3 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:


local Value = 0 local WorkspaceChildren = workspace:GetChildren() for i = 1, #WorkspaceChildren do local Child = WorkspaceChildren[i] Value = Value + 1 Child.Name = Value end

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

0
? Zeuxulaz 148 — 3y

2 answers

Log in to vote
1
Answered by 3 years ago

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

for i = 1, 10 do
    print(i)
end -- 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:

local Fruits = {"Apple", "Banana", "Orange"}
print(#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 — 3y
Ad
Log in to vote
1
Answered by
sngnn 274 Moderation Voter
3 years ago

i would be the current number the loop is at.

If you ran this in the command bar:

for 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 — 3y

Answer this question