What is v and in pairs?

So in scripts I see parts with
‘In_,v in pairs’ but I never know how it works,when to use and what it is.

Can someone explain pls? TY!

Edit:
I’ll get straight,I’m trying to understand this script

for _, v in pairs(list:GetDescendants()) do
	if v:IsA("TextButton") and table.find(allowedButtons, v.Name) then
		v.MouseButton1Click:Connect(function()
			target = v.Name
1 Like

Basically thats a loop, thats all there really is to it

It’s a for loop which can be read up on here https://education.roblox.com/en-us/resources/pairs-and-ipairs-intro it goes through each element of an array or dictionary without needing to set starting or ending points.

Feel free to read up on it through the article I sent

for i, v in pairs(table)

This for loop is used to read every single value of the table from one to another. The “i” is the index for example:


local table = {1, 6, 9, 2}

The number 6 is at the index of 2.

The “v” is the value in the table like 1,6,9 or 2.

1 Like

in pairs is a type of loop that is specifically for tables and dictionaries. It returns 2 things for each iteration, the index it is currently in if it’s a table, or if it’s a table, it returns the key it’s currently on, and the value in that index/key.

Examples

local fruits = {"Banana","Apple","Mango"}

for i,v in pairs(fruits) do
   print(i,v) --Prints 1 Banana then 2 Apple and then 3 Mango
end

For dictionaries

local dict = {
    ["Apple"] = "Delicious",
    ["Banana"] = "A banana"
}

for i,v in pairs(dict) do
   print(i,v) --Prints Apple Delicious and Banana A banana
end

you can name i and v anything you want, typically people use _ to name the first return because they have no use for it, if they do have a use for it, they’ll give it a proper name

I suggest watching the video made by @Alvin_Blox to fully understand for i, v in pairs() do

Edit: By the way, the i and the v could be anything, as said in the video, but we just normally use i and v, bexause i stands for index and v stands for value but I could change it to for minecraft, roblox in pairs() do if I wanted to. The name of the table that you want to loop through goes into the brackets/parenthesis, @Alvin_Blox also explains how this could be useful in developing too, he really doesn’t leave you in the dark.

In simple terms, it’s a loop that can read through each item in a table. The script you’ve shown will loop through each item that is parented under the ‘list’ then, it’ll check if it’s a text button, and it is in a table of allowed buttons, before creating a clicked event which sets the ‘target’ to the name of the current item in the table.

Imagine you were driving down a street and counting each house as you went. The number you’re at would be the i and the house itself would be v, and the street is the table. The i and v get their names just from “index” and “value”. They can be renamed to anything.

3 Likes