What is i, v in pairs() _, in pairs() etc

hi devs, i was wondering how i, v in pairs works and what it does because i see a lot of people using it and i don t know why and how it works can someone explain?

4 Likes

for i,v in pairs() is basically a loop, which loops through a parent or whatever. You use it when you want to get all the children in a parent.

The i (index) is the instance you want to loop through. (you can also loop through tables)

While v (value) is the child, or key in a scenario where you loop through a table.

Example:

for i,v in pairs(workspace.Folder:GetDescendants())

if v:IsA("Part") then
v:Destroy()
end
end

That would destroy every part that exists in the parent.

The workspace.Folder:GetDescendants() is the parent i want to loop through.

You can use GetDescendants() or GetChildren().

3 Likes

They’re both for iterating (looping through) elements of tables. However, the main difference is that ipairs is for indexes and pairs is for key, value pairs. For example:

{ "hi", "3", "string", "etc." }

This, put through ipairs would return i as “index” (e.g: 1, 2), the number of the spot of the element and v as “value” (e.g: “hi”, “3”) the actual element.

In contrast, for a table like:

{
    ["key"] = "value",
    ["anotherKey"] = "anotherValue",
}

You’d have to use pairs, which returns k (or i) as the “key” (e.g: “key”, “anotherKey”) and its respective value (e.g: “value”, “anotherValue”). You can read further on this here.

1 Like

it easily allows u to sort stuff out and makes the code way shorter, for example, if u have a model with 50 parts and want to change all their colors, u dont need to do it one by one, as u can use for i, v loops to change them all like this. for i, v in pairs(model:GetChildren) v.Color = (red)
v is the thing that is currently being used. it can be used for tables like
for i, v in pairs(table) do
if v.Name == (whatever ur looking for) then
print(1)
end
end

there are more uses like for i = 1, 100 or something but I think that the ones I mentioned here are the most used

2 Likes

Instance:GetDescendants() returns a table with a list of Instances inside of it, not the ‘Parent.’ The loop iterates through lists

1 Like

Looping through a table. pairs randomly loops it whilst ipairs loops in the order of the table. Tables can be used to store almost anything.

local ExampleTable = {"hey", Vector3.new(0,40,0), workspace.Part.CFrame, 3}
local WorkspaceChildren = game.Workspace:GetChildren() -- this is also a table
local gameDescendants = game:GetDescendants() -- table

for i, v in pairs(ExampleTable) do
   print(v) -- prints the elements of the table in a random order
   print(i) -- what element the table is currently on, for example, the number 3 inside our table would be iteration 4
end

for i, v in ipairs(ExampleTable) do
   print(v) -- prints in a orderly fashion, {"hey", Vector3.new(0,40,0), workspace.Part.CFrame, 3}
   print(i) -- prints the current iteration just like normal pairs
end
3 Likes

Yes I know that I just didn’t know how to say it.

1 Like

You don’t need to use these functions anymore.

2 Likes

pairs() are no longer useful in Roblox because of Luau. There is this thing called generalized iteration which means tables by default are iterable without the need of an iterator function. ipairs(), on the other hand, is still used because it has the unique property of iterating only the array portion of a table.

All of these are ways to iterate a regular table:

for k, v in next, t do

for k, v in pairs(t) do

for k, v in t do

ipairs will only iterate the array portion:

for k, v in ipairs{1, 2, 3, a = 'a', b = 'b', c = 'c'} do print(k, v) end
--> 1 1
--> 2 2
--> 3 3

Generalized iteration will iterate through everything but guarantees the chronological iteration of arrays (arrays will always be iterated first and in the correct order)

for k, v in {1, 2, 3, a = 'a', b = 'b', c = 'c'} do print(k, v) end
--> 1 1
--> 2 2
--> 3 3
--> a a
--> b b
--> c c

While regular pairs() does not guarantee the order. It will iterate through everything just like generalized iteration but in any random order.

TL;DR you should only be using generalized iteration for iterating tables, but ipairs() if you only want the array part of tables. pairs() is dead

2 Likes

for loops can iterate over a counter or tables. The one you are referring to iterates over tables.

Iteration is looping by the way.

i stands for index or iterations (I prefer index)
v stands for value

Tables just store values. No extra things. You can still use pairs though.

local tableObject = {
    "wow";
    "noice";
}

for index, value in pairs(tableObject) do
    print(index, value)
end

--[[Output:
    1, wow
    2, noice
]]

Arrays use index-value pairs.

local array = {
    [1] = "hello"; --1 is the index and "hello" is the value
    [2] = 4; --2 is the index and 4 is the value
}

for index, value in pairs(array) do
    print(index, "in array is", value..".")
end

--[[Output:
    1 in array is hello.
    2 in array is 4.
]]

Using for loops on arrays and tables act essentially the same.

Dictionaries use key-value pairs.

k stands for key.
Keys are a string that can be used to access the value.

local dictionary = {
    key1 = "coolvalue"; --key1 is the key and "coolvalue" is the value
    key2 = 6; --key2 is the key and 6 is the value
}

for key, value in pairs(dictionary) do
    print(key)
    print(value)
    print("---")
end

--[[Output:
    key1
    coolvalue
    ---
    key2
    6
    ---
]]

As for _, it just skips the argument. Good practice if you don’t need the index/iterations/key/value.

1 Like

Here’s the most Nathan-ish way to explain as simple as possible.

For ipairs()

local myTable = {
  "Foobar",
  "Foobaz"
}

for i, v in ipairs(myTable) do
  print(i.." "..v)
end

for - For each (everytime, you could say)

i - Iterations, it correlates to how many times it has ran or the current index in the table (I’ll explain more), it’s variable assignment you can change it to any for your comfort

v - Value, aka the items like “Foobar” and “Foobaz” in the table, it is just myTable[i], it’s variable assignment you can change it to any for your comfort

do - This is when you tell the script to run the functions with the specific variables you’ve assign

You’re basically saying:
For each values (v) along with iteration (i) to reach it found in the table (but return me the iteration, which is ipairs) do this/these action(s) and end

When you run the code, it’ll output

1 Foobar -- i is 1 because it is first, v is "Foobar"
2 Foobaz -- i is 2 because it is second, v is "Foobaz"

For pairs()

local quotes = {
  ["Class S"] = "Lorem Ipsum",
  ["Class A"] = "The Quick Brown Fox"
}

for k, v in ipairs(quotes) do
  print(k.." "..v)
end

Since you’ve read the ones above, I assume you know enough to be using pairs().

k - Key, to open a certain value of the table, it’s variable assignment you can change it to any for your comfort, can be any kind of var like string, num, bool

v - Value, like above said about pairs(), but it’s quotes[k]. It’s variable assignment you can change it to any for your comfort

You’re basically saying:
For each values (v) along with the key (k) found in the table (but return me the key that allows me to access it, which is pairs) do this/these action(s) and end

When you run the code, it’ll output

Class S Lorem Ipsum -- k is "Class S" because key is, v is "Lorem Ipsum"
Class A The Quick Brown Fox -- k is "Class A" because key is, v is "The Quick Brown Fox"

On side notes

If you ever see stuff like for _, it’s just a way to say the iteration/key is unnecessary so drop it. It might free up some memory.

2 Likes