Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Can someone explain for i,v in pairs for me? I don't understand it.

Hey I've been struggling on making games and I heard my fix was something called for i,v in pairs so can someone explain it to me?

Thank you

3 answers

✓ Accepted answer

Basically it's a for loop. You're looping through an array, and if you don't know what an array is, go learn that first.

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

Basically what the code above does is loop through the array. Let's say that you had an array {a,b,c}. If you replace the list variable with this array, the following things will happen.

Lua sees your code and goes, this person wants to loop through this array. Well, to know how many times it'll loop, we just count the number of items in the array. There's a, b, c, so three items. We'll loop through the code inside 3 times.

Then, it starts looping. The first time it goes through, it sets i to 1, to indicate that this is the first time the loop is happening. then it sets v to the i-th index in the array. Since i is 1, v become list[1], which is a. Now it runs the code inside. It sees that you want to print v, so it prints the variable v, which has a value of a, so it prints a in the output. The code inside is done executing, so it moves on to the second time it loops through.

Similarly to the first time, lua sets i to 2, and v to list[2], which is b. It then prints b. The same thing happens the third time.

After the third loop is done, lua sees that all items in the array have been looped through, and carries on with the rest of the script. In the output you'll see

a b c

Since that's what you printed out each time.

Print isn't the only thing you can do with for loops, and for more information you can check out the dev forum page here: https://developer.roblox.com/en-us/articles/Loops

1 comment

0

Thanks

Sometimes you want to do something to all the elements of a table, or a list. Let's say we have the following list:

ages = {
	jason = 5,
	tom = 7,
	harry = 4,
	joe = 5,
	dick = 6,
}

Those boys all have the same birthday and now you want to add one to their ages. You can simply do this:

ages.jason = ages.jason + 1
ages.tom = ages.tom + 1
ages.harry = ages.harry + 1
ages.joe = ages.joe + 1
ages.dick = ages.dick + 1

Now if you don't know why I did ages.name, since they're all in a table I have to do ages.jason otherwise I can't access their ages otherwise.

However, there's a shorter way to do this and it's easier too:

for i,v in pairs(ages) do
	v = v + 1
end

-- as an aside you can rewrite v = v + 1 as v += 1, they mean the same thing.

Does that look confusing? It might be a bit.

The "i" in for i,v means index and refers to each boy's name and the "v" means value and refers each boy's age. When I say for i,v in pairs() I really am saying, for each boy in the table, and his age do this-this-and-that.

1 comment

0

P.S: A table will always need to be in the brackets after pairs().

It's an ability for you to go through a table. For example, let's say you want to check everything in a table with ease, you can do that with in pairs.

The i in the in pairs loop represents an index of a table. For example, the first item in a table would have an index of 1, the second would have an index of 2, etc. The v in the in pairs loop represents the value itself. When looping, the v would always be equivalent to the nth item in a table, where n is the current index.

As a note, you do not have to use i and v, you can use anything you want, as long as you use it when referencing it.

ex)

Here is an example of printing out a value of a table.

local myTable = {"hello", 5, true, false, game.Workspace.Baseplate}

-- print every item in a table'

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

--[[
Should print:
hello
5
true
false
Baseplate -- (or whatever it prints if it is an object)
]]

Here is an example where you would use the indexes.

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

for i, v in pairs(numbers) do
	if numbers[i] < 4 then
		table.remove(numbers, i)
		-- Should leave only the numbers 4 and 5
	end
end

Finally, you can also use a function GetChildren, GetDescendants, or GetPlayers (for the players service) to convert an item's children into a table.

ex)

for i, v in pairs(workspace.Folder:GetChildren()) do
	if v:IsA("Part") then -- checking if the item we are looping to is a part
		v.Material = "Wood" - making every "Part" in a folder a wood material
	end
end

Hope this helps.

This discussion is preserved as it was. New answers, comments, and votes are closed.