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

What is the third type of a "for loop"?

I know 2 types of For Loops:

for i = n,n,n --The one with three numbers
	-- (Insert code here)
end
for i = n,n -- The one that increments by one
	-- (Insert code here)
end
--But, I don't know what this is:
for i = 1, #variables -- #idk_what_this_is
	-- (Insert code here)
end

Can someone explain to me the third type of for loop that I see in some scripts?

2 answers

✓ Accepted answer

its the same as the second one, normally people use this if they want to loop through a table

the # means the amount of indexes in a table

local table = {a,b,c}
for i =1,#table do
print(i)
end

-- is the same as
for i = 1,3 do
print(i)
end

but people use for i =1,#table because sometimes they dont know how long a table is

1 comment

0

Oh, okay! Thanks for clarifying what it is. I didn't find what it meant on the Roblox Wiki until your answer came up. Thanks!

Aazkao almost nailed it on the head. It is just like the second one, however the 3rd number represents the step. You can read more about it in our glossary. The step is the increment it will count to get from one number to the next. It can be negative, but the 2nd number would have to be lower than the first.

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