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

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

Asked by 5 years ago
Edited 5 years ago

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

Log in to vote
0
Answered by
aazkao 787 Moderation Voter
5 years ago
Edited 5 years ago

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

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! Deson00 4 — 5y
Ad
Log in to vote
0
Answered by
Shawnyg 4330 Trusted Badge of Merit Snack Break Moderation Voter Community Moderator
5 years ago

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.

Answer this question