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 6 years ago

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

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

like, what is the function of v in pairs?

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

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 6 years ago
Edited 6 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:

1for var=exp1,exp2,exp3 do
2    something
3end

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

01for i = 1, 5 do
02    print(i)
03end
04-- output --> 1, 2, 3, 4, 5
05for i = 4, 7 do
06    print(i)
07end
08-- output --> 4, 5, 6, 7
09for i = 2, 4, .5 do -- using exp3
10    print(i)
11end
12-- 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:

01for i = 3, 1, -1 do
02    print(i)
03end
04-- output --> 3, 2, 1
05for i = 3, 1, -.5 do -- using a decimal negative number
06    print(i)
07end
08-- output --> 3, 2.5, 2, 1.5, 1
09for i = 1, 5, -1 do -- would not work because you cannot count up to five by -1
10    print(i)
11end

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

1local array = {"a", "b", "c"}
2for i,v in pairs(array) do
3    print(i, v)
4end
5-- 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

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

This also works with dictionaries as shown in this example:

1local dictionary = {
2    ["box"] = "wood"
3    ["jug"] = "plastic"
4    ["spoon"] = "silver"
5}
6for item,material in pairs(dictionary) do -- using names that make sense in the situation
7    print(item, material)
8end
9-- 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:

01local array = {"happy", nil, "bob"}
02for _,v in ipairs(array) do
03    print(v)
04end
05-- output --> happy
06-- the loop stops at the nil value in the array
07local dictionary = {
08    ["box"] = "wood"
09    ["jug"] = "plastic"
10    ["spoon"] = "silver"
11    ["hmm"] = nil
12    ["key"] = "iron"
13}
14for item,material in ipairs(dictionary) do
15    print(item, material)
View all 24 lines...

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 — 6y
0
Thank you so much! User#21908 42 — 6y
0
DUDE.... Too many words ;-; greatneil80 2647 — 6y
0
Thank you EliteRayGawnX2 124 — 6y
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 — 6y
0
No problem! Glad I could help. User#21908 42 — 6y
Ad
Log in to vote
0
Answered by 6 years ago
Edited 6 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:

1local children = workspace.Part:GetChildren() -- returns a array
2 
3for i,v in pairs(children) do
4    if v:IsA("BasePart") then -- checks if the object is a BasePart
5        v.Transparency = 0.5 -- changes the transparency
6        print(v.Name, i) -- prints the name of the object and the index of it
7    end
8end

For Loop

Log in to vote
0
Answered by 6 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.

1for i,v in next, Table do
2    print(i,v)
3end
4--Generic Loop
5 
6for i = 1,#Table do
7    print(i) -- Index's the # of values found in the table
8    print(Table[i]) -- This indexs the values of the table
9end
1
Don't use next. SummerEquinox 643 — 6y
0
Using next is perfectly fine User#19524 175 — 6y
0
Using next is perfectly fine User#19524 175 — 6y
Log in to vote
-2
Answered by
Cyrakohl 108
6 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 — 6y