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

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

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).

1 comment

0

?

2 answers

✓ Accepted answer

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".

1 comment

0

Thanks! You made it very clear.

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.

1 comment

0

Thanks for the explanation!

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