Need help with for i,v in pairs

So I am having a very hard time understanding on how to use for i,v in pairs from the docs or yt vids as I dont understand when I will use the i or v inside the code

1 Like

Here:


for i,v in pairs(table1) do
      -- Your code here
end
-- If you do, it just loops through everything in a table.
-- Example:

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

It’s not that hard to understand. So, let’s start!

for i, v in pairs(stuff:GetChildren()) do
    -- Code
end

So, what does this mean?

You can write anything in the place of i and v. Normally, they mean index and value. So, let’s say you are trying to customize all the parts just by 1 script. So, for loop is the best.

i = Index;
v = Value;

And in pairs means pairs. (Can’t explain this part properly, sorry!)

So, normally you can do it like this:

for index, value in pairs(workspace:GetChildren() --[[Looping through only the children of the workspace]]) do
    if value:IsA("BasePart") then -- Checking if the value we looped through is a base part or not.
        -- Kill the player
    end
end
for index, value in pairs(workspace:GetDescendants() --[[Looping through the descendants of the workspace]]) do
    if value:IsA("BasePart") then -- Checking if the value we looped through is a base part or not.
        -- Kill the player
    end
end

a for i, v in pairs() do loop is able to list all children or descendants of an Instance (depending if you use :GetChildren() or :GetDescendants()) or a table’s arrays, as I will show below.

Let’s say I have a table.
And my table contains some of the players’ scores.

local scoreTable = {
Nakermo = 45,
AridFlights1 = 58,
Builderman = 999,
}

Using a for loop, I will be able to check the scores and identify the Players as index and their Score as value.

local scoreTable = {
   Nakermo = 45,
   AridFlights1 = 58,
   Builderman = 999,
}

for index, value in pairs(scoreTable) do
   print("Player " .. index, "has a score of " .. value)
end

--OUTPUT
Player Nakermo has a score of 45
Player AridFlights1 has a score of 58
Player Builderman has a score of 999

You can rename index and value as whatever you want.
Doing this on an instance, it would print out the instance descendant VALUE and their numeric position INDEX.

If you were to do, for i, v in ipairs() do, it would print out the instances in a numeric order.

3 Likes

code is wrong here
the first line in both scripts has to end in ") do"
you forgot a parentheses and a do

Nope I didn’t. Scroll to the right, you’ll see it.

Then what do we do with the index usually? So value is the player or thing we are referencing?

He has done the )do just scroll to the very right

I realized you put it after the comment, that is really evil not gonna lie

you hurt my eyes

1 Like

So, the index is not always used (But it depends on when you need it). Let’s say I’m AridFights1 and you’re WaterFrontDev. Since my name starts with A and it comes at first so my index will be 1, and yours will be 2 since you’re behind me.

OHHHHHHHHH I see thanks :))) But can you give more examples so that I can understand it better?

1 Like

You can have your loop and not mention the index in your code at all if you don’t need to. value is the property/instance that comes as a result from the loop. Saying like:

for i, v in pairs(folder:GetChildren()) do
end

is the same as

for every, instance inside of (aRandomFolder:GetChildren()) do, getting every child inside of that folder, or descendant, depending always on what you are using. If you are confused, I’m sorry, I’m just trying to make this a little bit more simple.

Hm I see , so you can use like print(i) and print(v.Somepart.Name) as a whole as i is the table and v is the things inside the table?

1 Like

Yes, and i will most likely print out a number, you can also check for parts like:

for i, v in pairs(workspace:GetChildren()) do
   if v:IsA("Folder") then
      print(v) --Would print out all folders
   end
end

It’s not exactly a table. It’s the position of the value (Which is called Index).

If also that value was a basepart, we could change its properties, we could do,

v.Name = "RandomName"
v.Parent = workspace.Folder

and so on

1 Like

Ok so basically whatever is inside the brackets after pairs

So if we create a clone then we could use this suppose v:Clone()
v.Name = “something”
v.Parent = workspace

Yes. After the pairs we loop through the item we want to loop through. And the position of that value is the position.

1 Like

Make it a variable first, then clone it.