What is iv in pairs?

Hello Developers ! So I have been watching and learning through out Alvinblox’s tutorial on YouTube about “for iv in pairs” I still don’t understand what is it used for or how does it work because I’m kinda puzzled.

Sorry if I don’t have much statement/question on my topic to cover with.

I’m just a “Noob scripter” asking for help on devforum so I could get a better understanding and learn from you guys of the code itself. Thanks :kissing_heart:

17 Likes

do you mean

for i,v in pairs(tablenane) do
      -- blah blah
end

if you do, it just loops through everything in a table.
example;

local noob = game.Workspace.Noob:GetChildren()
for i,v in pair(noob) do
        print(v .. " has been destroyed.")
        v:Destroy()
end
15 Likes

for i,v in pairs() do
is a loop that iterates trough all values on a table/dicitonary/array returning the index and value.
example:

local table = {"hi"}
for i,v in pairs(table) do
print(i,v) -- 1 hi
end

Why does it prints 1 and hi?
1 is the index, the position of the value in the table.
hi is the value, and we are printing it.

11 Likes

What it’s about is a for loop. What it’ll do is loop through a given table, children of a folder, etc. and find out what’s there. For example:

for i,v in pairs(game.Players:GetPlayers()) do
    if v:FindFirstChild("WooleyWool") then
      print("this is wooleywool's tag")
    end
end

What it’s doing si looping through all of the players and finding who has the tag, WooleyWool. Look here for more info: Introduction to Scripting | Documentation - Roblox Creator Hub

3 Likes

I will make a simple explanation:

i is referencing index and v is referencing value, however, you do not always need to name it i and v, it could be _ and w.


local Table = {1,2,3,4,5}

for i, v in pairs(Table) do 
print(i) -- The output will be 1,2,3,4,5 because there are 5 positions in the table.
print(v) -- The output will also be 1,2,3,4,5.
end 

You could also get items from a folder:

for _, v in pairs(Folder:GetChildren()) do 
if v.Name == "HelloThere" then -- v would be the items in the folder, so that could be a Part, Model, Another folder.
print("here") 
end
end 
5 Likes

Yea so I definitely recommend you searching before posting because there are a bunch of tutorials explaining the i, v in pair loop. Not just Alvin_Blox.

2 Likes
for i,v in pairs() do

Can be used for alot of things, for example if you want to loop through something, for example if you want to track if a player is near something, you can loop through the wokspace.FolderName:GetChildren() and then use basic scripting from there if you would like to learn more info check out https://developer.roblox.com/en-us/articles/Loops

1 Like

I’m pretty sure you have to print v … “has been destroyed.” first, as it would be nil after it got destroyed (correct me if I’m wrong).

For the OP; it’s basically just something that loops through a table;

local myTable = {
"Hi",
"Lol",
"Idk",
"Idk2"
}
for i,v in pairs(myTable) do
     print(tostring(i) .. " - " .. v)
end

i basically stands for index and v stands for value. You can change both of them if you want to, basically just like variables.

A little bit more advanced: If it’s an Array, use ipairs instead of pairs so it loops through it in the correct order (an array is something like this: local myArray = {“Hi”, “lol”, …} and a dictionary something like this local myDictionary = {indexName = “Something”, indexName2 = “Something2”})

local myArray = {
"Hi",
"Lol",
"Idk",
"Idk2"
}

for i,v in ipairs(myArray) do
     print(tostring(i) .. " - " .. v)
end

When printing out i (a number) right before/after v (a string, in our case), you have to call the tostring() function on i, which basically converts the number to a string correct me if I’m wrong.

4 Likes

In Roblox, “IV” stands for “Item Value.” It is a property of an item that represents its value or rarity. In a pair of parentheses after “IV,” you might see a number or a variable. The number or variable refers to the IV of a specific item.

For example, if you have a function called “GetIV” that takes an item as an argument and returns its IV, you might see code like this:

local item = game.Workspace.Item
local itemIV = GetIV(item)

In this code, the variable “itemIV” will be set to the IV of the item represented by the “item” variable. The value of “itemIV” might be a number, a string, or some other data type, depending on how the “GetIV” function is implemented.

1 Like

i believe he meant

for i, v in pairs(tablename) do
     --stuff
end

cuh what are you talking abt what rarity dawg

Why would you use a _ instead of an i?

Incase you do not need an index.