Wehn we should use for i,v in pairs

wehn we should use for i,v in pairs

3 Likes

That will loop a code for each of the contents of pairs(Object). ex:

for i,v in pairs(Object:GetChildren) do
   -- i = The count of the current object (the ith object)
   -- v = The current object
end
1 Like

Hello!

For i, v in pairs() is commonly used when you need to loop through tables.
Here is the link to the doc pages if you want to read more into it: Tables | Documentation - Roblox Creator Hub
Example:

local folder = script.Parent

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

i is the count of the current object and v is the object/instance you are searching for.
i is not always needed, though, for we just use an underscore __.

for _, v in pairs() do

end)

Here’s something important to remember: use pairs() for tables (like getting the children of an object) and ipairs() for dictionaries.
If you’re not sure what a dictionary is, read this: Tables | Documentation - Roblox Creator Hub
It’s basically a table, but the data inside of it has assigned values.

Hope this helps and have a wonderful day! :slight_smile:

2 Likes

I won’t explain it to you in a professional way but in the way I understand for inpairs, and they are basically used to navigate tables as other people have told you, or objects too, I will give you examples in which I use this loop type:

In the first case I used it for a button that generates a group of npc, for this I used a for in pairs that went through the replicated storage which was where these npc were, and I passed them to the workspace, the code is the following:

--pp its a proximity prompt
local pp = script.Parent

pp.Triggered:Connect(function()
	
	for i,v in pairs(game.ReplicatedStorage:GetChildren()) do
		
		v.Parent = game.Workspace
	end
end)

Another detail to take into account is that if you want it to take certain children and not take the meta table or the entire instance, you can use the :IsA function, which in use would look something like this "if v:IsA(“part”) then " and is usually used in this type of loops so that it takes only certain instances of a certain type of class

The second case in which I have used them is to remove all of a character’s clothing and change it for another, here is the code example:

for i, v in pairs(char:GetChildren()) do

		if v:IsA("Accessory") then

			v:Destroy()

		end
		if v:IsA("Hat") then

			v:Destroy()

		end
		if v:IsA("BodyColors") then
			
			v:Destroy()
			
		end
		if v:IsA("Pants") then
			
			v:Destroy()
			
		end
		if v:IsA("Shirt") then

			v:Destroy()

		end
		if v:IsA("ShirtGraphic") then

			v:Destroy()

		end
	end

As you can see, I also made use of the function that I told you about a moment ago, this so that the loop would only eliminate instances of the accessory type, hat, body color, etc.

edit:Apologies if there are grammatical errors, I am Spanish and I was using the translator

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.