What is for i,v in pairs?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? I want to learn How to use for i,v in pairs.

  2. What solutions have you tried so far? Youtube

Hey! I want to learn how to use for i,v in pairs() do
I have been a scripter for a while and have created so many things but I have not been able to understand for i,v in pairs. Can someone explain to me what it means and How I can use it?

12 Likes

It’s a type of for loop:

5 Likes

It iterates through a given table. Also wrong category.

Pairs is an iterater function that uses next(). When looping with for I,v, I will be the index and v will be the value associated with that index.

1 Like

i, v in pairs is basically looping through a group of items. The most common way I do this is :GetChildren(). It makes a table and v is the loop that is created. You can also loop through a table. :GetChildren() creates a table.

For example:

for _, WorkspaceItems in pairs(workspace:GetChildren()) do
  print(v.Name)
end

This will print all the names in the Workspace. It also doesn’t need to be for i, v. I do _, WorkspaceItems here. i basically tells you what is in the table. If you use :GetChildren() like me, it will do how many times looped. I usually dont use it.

Hope this helps you!


Also, I believe this belongs in #help-and-feedback:scripting-support . Please move it there.

5 Likes

Thank you. Also, I had no idea what category to put it in so sorry.

1 Like

You should move this to #help-and-feedback:scripting-support

1 Like

I use it to loop through folders. Currently, my main use for them is looping through a folder and putting the name of the objects in a table so I can save it when the player leaves the game.

1 Like

All tables are written as {index = value, anotherIndex = anotherValue}

local newTable = {“Hey”, “Everyone”}

is actually automatically converted into

local newTable = {1 = “Hey”, 2 = “Everyone”}

So all tables are just a bunch of pairs of indexes and values.

When you do
for i,v in pairs(tablename) do
then you’ll loop through the contents of ‘tablename’ and get the index (i) and value (v) at the same time.

Example:
local newTable = {“Hey”, “Everyone”}

for i,v in pairs(newTable) do
print(i,v)
end

would output:
1 Hey
2 Everyone

Hope this helps, I’m not super sure what you’re asking for.

23 Likes

It’s a method you can use to iterate through something.
It’s a special one though.

Let’s look through the differences,

  • Pairs() does it in a regular way.
  • iPairs() does what Pairs() does, but the opposite way, meaning it’s going down > up, instead of up > down.
  • next is like a operator in Lua, meaning that it is not created by Roblox, it basically does what Pairs() and iPairs() do, but it’s not a function, and it does it in a regular way.
Example
local ExampleTable = {}
-- Both Pairs() and iPairs() can only be used with Tables.

for i,v in Pairs(ExampleTable) do
      -- i = the position where it is in the table.
      -- v = the actual object.
end
-- The code above, the same thing you can do with iPairs().
-- For me, I use next instead of Pairs() or iPairs()

for i,v in next,ExampleTable do
      -- does same thing, but iterates in a normal way.
end

For the first argument in any iterating function you use, it should be a table, if not then it will error.

Forgot to mention, if there’s nothing in the table, the iterator wont run.

3 Likes

Thank you a lot! I will use this in the future!

I’ll explain it the best I can.

So say you have a table, it looks something like this.

local tbl = {"Cow","Milk","Pop","Oink"}

You can use

for i,v in pairs() do

to cycle through the things in the table.


Assuming you are using i and v, v will give you the actual value. i will give you indentification/place in the table where it is. that space between the brackets is where the table goes in.


Let’s put this in a script.

local tbl = {"Cow","Milk","Pop","Oink"}

for i,v in pairs(tbl) do
	warn(v)
	warn(i)
end

The Output

image


Looking at this, it shows you the value that v stands for and i, the place where it is.

After looking at JarodOfOrbiter’s comment I think I should add a little more.


Let’s try assigning the values in the scripts with a letter.

local tbl = {['a'] = "Cow",['b'] = "Milk",['c'] = "Pop",['d'] = "Oink"}

for i,v in pairs(tbl) do
	warn(v)
	warn(i)
	warn("--------------------------------------------")
end

Output
image


Now, instead of getting numbers we get letter becuase we have assigned the values in the table with something.

Some applications would be cycling through players to check the number of coins they have, setting the value of parts in a folder, and many more.

Hope this helps!

6 Likes

None of that is correct.
ipairs iterates over arrays (1 = “hi”, 2 = “apple”)
pairs iterates over arrays and dictionaries both (test = “hi”, example = “apple”)
next is the same as pairs as far as you are concerned, but specifically it gets the next entry in the table.
next({test = “hi”, example = “apple”}, test) → example, apple
next is a function, pairs is a function, ipairs is a function. None of them are made by Roblox, they exist in vanilla lua.

pairs also returns next if you care.

local function pairs(t)
    return next, t
end

That’s pretty much an exact copy of pairs.

Edit: Also, from PiL, this is a replacement for ipairs. I think ipairs (like next) is handled in C-side so it is more efficient to use ipairs than this replacement, but this will do the same thing otherwise.

    function iter (a, i)
      i = i + 1
      local v = a[i]
      if v then
        return i, v
      end
    end
    
    function ipairs (a)
      return iter, a, 0
    end

next has no replacement, and if you want to get a value in a dictionary without knowing an exact key, you must use it.

Edit 2 since I can’t proofread and come up with more than one thought at a time:
you can use the ‘iter’ function I shared the same as you can next
for i, v in next, t do
for i, v in iter, t do
the difference being that next is still irreplaceable and iter doesn’t have the capability to iterate over string keys, only integral keys. In other words, ipairs and PiL’s iter function will only work on tables that look like this
{"test", "seven", "red car", game.Workspace}
next and pairs will both work on tables that look like this as well as the table above.
{brand = "Mazda", color = "White", milage = 100}

7 Likes

I only use next because I don’t have to put () in my code, unlike Pairs() and iPairs().
Thank you, overall though.

3 Likes