Like when I see other scripts with this loop, it doesnt make sense
1 | for i = 1 , v in pairs do --I think "i" is the number of times the loop should run? |
2 | wait() |
3 | end |
like, what is the function of v in pairs
?
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:
1 | for var = exp 1 ,exp 2 ,exp 3 do |
2 | something |
3 | 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,
01 | for i = 1 , 5 do |
02 | print (i) |
03 | end |
04 | -- output --> 1, 2, 3, 4, 5 |
05 | for i = 4 , 7 do |
06 | print (i) |
07 | end |
08 | -- output --> 4, 5, 6, 7 |
09 | for i = 2 , 4 , . 5 do -- using exp3 |
10 | print (i) |
11 | end |
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:
01 | for i = 3 , 1 , - 1 do |
02 | print (i) |
03 | end |
04 | -- output --> 3, 2, 1 |
05 | for i = 3 , 1 , -. 5 do -- using a decimal negative number |
06 | print (i) |
07 | end |
08 | -- output --> 3, 2.5, 2, 1.5, 1 |
09 | for i = 1 , 5 , - 1 do -- would not work because you cannot count up to five by -1 |
10 | print (i) |
11 | 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
1 | local array = { "a" , "b" , "c" } |
2 | for i,v in pairs (array) do |
3 | print (i, v) |
4 | end |
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
1 | local array = { "a" , "b" , "c" } |
2 | for _,v in pairs (array) do |
3 | print (v) |
4 | end |
5 | -- output --> a, b, c |
This also works with dictionaries as shown in this example:
1 | local dictionary = { |
2 | [ "box" ] = "wood" |
3 | [ "jug" ] = "plastic" |
4 | [ "spoon" ] = "silver" |
5 | } |
6 | for item,material in pairs (dictionary) do -- using names that make sense in the situation |
7 | print (item, material) |
8 | end |
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:
01 | local array = { "happy" , nil , "bob" } |
02 | for _,v in ipairs (array) do |
03 | print (v) |
04 | end |
05 | -- output --> happy |
06 | -- the loop stops at the nil value in the array |
07 | local dictionary = { |
08 | [ "box" ] = "wood" |
09 | [ "jug" ] = "plastic" |
10 | [ "spoon" ] = "silver" |
11 | [ "hmm" ] = nil |
12 | [ "key" ] = "iron" |
13 | } |
14 | for item,material in ipairs (dictionary) do |
15 | print (item, material) |
Links for reference:
For loops
This one is from the Developer wiki
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:
1 | local children = workspace.Part:GetChildren() -- returns a array |
2 |
3 | for 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 |
8 | end |
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.
1 | for i,v in next , Table do |
2 | print (i,v) |
3 | end |
4 | --Generic Loop |
5 |
6 | for 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 |
9 | end |
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
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?