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

how does an advanced for loop actually work? [closed]

Asked by 5 years ago

Like when I see other scripts with this loop, it doesnt make sense

for i=1, v in pairs do --I think "i" is the number of times the loop should run?
wait()
end

like, what is the function of v in pairs?

0
No problem! Glad I could help. User#21908 42 — 5y

Locked by User#5423

This question has been locked to preserve its current state and prevent spam and unwanted comments and answers.

Why was this question closed?

4 answers

Log in to vote
6
Answered by 5 years ago
Edited 5 years ago

In your question, you are trying to combine two different types of for loops in one. The Programming in Lua book says that, "A numeric for has the following syntax:

for var=exp1,exp2,exp3 do
    something
end

" In the explanation, var stands for variable. This form defines var to be the number you want to start at. The second number, exp2, is how far you want the loop to go. The exp3 is optional and it is the amount your variable increases by every loop. The default value for exp3 is one. For example,

for i = 1, 5 do
    print(i)
end
-- output --> 1, 2, 3, 4, 5
for i = 4, 7 do
    print(i)
end
-- output --> 4, 5, 6, 7
for i = 2, 4, .5 do -- using exp3
    print(i)
end
-- output --> 2, 2.5, 3, 3.5, 4

You can even loop backwards with a negative value in the exp3 spot. The thing is, you will need to remember to use a larger value in the exp1 spot than the exp2 spot or else you will get an error. The reason it will throw an error is that you cannot count from something like 1 to 20 by -1. You can, however, count from 20 to 1 by -1. Here is an example:

for i = 3, 1, -1 do
    print(i)
end
-- output --> 3, 2, 1
for i = 3, 1, -.5 do -- using a decimal negative number
    print(i)
end
-- output --> 3, 2.5, 2, 1.5, 1
for i = 1, 5, -1 do -- would not work because you cannot count up to five by -1
    print(i)
end

The next kind of for loop is the generic for loop. The book says, "The generic for loop allows you to traverse all values returned by an iterator function." ipairs and pairs would be examples of iterator functions. This allows you to loop through dictionaries and arrays (or tables as they are called in Lua). An example would be

local array = {"a", "b", "c"}
for i,v in pairs(array) do
    print(i, v)
end
-- output --> 1, a; 2, b; 3, c

Generally, these letters are used because i stands for index and v stands for value. If you don't want to call them that you don't have to. I recommend using names that make sense based on the situation you are using it in. Often in Lua it is convention to use _ as i if you are not going to use it. For example

local array = {"a", "b", "c"}
for _,v in pairs(array) do
    print(v)
end
-- output --> a, b, c 

This also works with dictionaries as shown in this example:

local dictionary = {
    ["box"] = "wood"
    ["jug"] = "plastic"
    ["spoon"] = "silver"
}
for item,material in pairs(dictionary) do -- using names that make sense in the situation
    print(item, material)
end
-- output --> box, wood; jug, plastic; spoon, silver

ipairs works similar to pairs, but stops at a nil value in an array. Here is a simple example:

local array = {"happy", nil, "bob"}
for _,v in ipairs(array) do
    print(v)
end
-- output --> happy
-- the loop stops at the nil value in the array
local dictionary = {
    ["box"] = "wood"
    ["jug"] = "plastic"
    ["spoon"] = "silver"
    ["hmm"] = nil
    ["key"] = "iron"
}
for item,material in ipairs(dictionary) do
    print(item, material)
end
-- output -- > box, wood; jug, plastic; spoon, silver
-- the loop stops at the nil value in the dictionary as well
local array = {"happy", "good", "rocket"}
for _,v in ipairs(array) do
    print(v)
end
-- output -- > happy, good, rocket
-- it works the same as pairs when there are no nil values

I hope this helps and have a great day scripting!


Links for reference:

2
Absolutely awesome explanation Phlegethon. Hopefully OP can understand it with such a well-made answer. SummerEquinox 643 — 5y
0
Thank you so much! User#21908 42 — 5y
0
DUDE.... Too many words ;-; greatneil80 2647 — 5y
0
Thank you EliteRayGawnX2 124 — 5y
View all comments (2 more)
0
This was great! I can’t rep+ but you’d get one if I could. Helped my understanding, especially of the use of pairs tremendously. DinozCreates 1070 — 5y
0
No problem! Glad I could help. User#21908 42 — 5y
Ad
Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

a for loop is a loop that literates through a table. the v is the variable or value which is the object that the loop iterated over, and "i" is the index of the object if you know arrays like myTable[1]. You can use a for loop if you wanted to loop over several objects to make a change to all of them such as:

local children = workspace.Part:GetChildren() -- returns a array 

for i,v in pairs(children) do
    if v:IsA("BasePart") then -- checks if the object is a BasePart
        v.Transparency = 0.5 -- changes the transparency
        print(v.Name, i) -- prints the name of the object and the index of it
    end
end

For Loop

Log in to vote
0
Answered by 5 years ago

The difference between the numerical loop (for loop), is that it's commonly used to iterate through a table with values, aka array, thus if you're using a dictionary (assigning variables in a table), you use the generic loop, which iterates through the dictionary to index the #x of variables, also call values.

for i,v in next, Table do
    print(i,v)
end
--Generic Loop

for i = 1,#Table do
    print(i) -- Index's the # of values found in the table
    print(Table[i]) -- This indexs the values of the table
end
1
Don't use next. SummerEquinox 643 — 5y
0
Using next is perfectly fine User#19524 175 — 5y
0
Using next is perfectly fine User#19524 175 — 5y
Log in to vote
-2
Answered by
Cyrakohl 108
5 years ago

For I,v in pairs(table) consists of two things index,value. As you can see I = index v=Value. Pairs iterates through the table getting every value inside

0
That’s one use of a for loop there are plenty more uses which can be found here http://robloxdev.com/articles/Roblox-Coding-Basics-Loops Cyrakohl 108 — 5y