i want to know what for i,v in pairs does cuz i and maybe others wanna use it in the future.
what is for i,v in pairs and why do people use it?
3 answers
pairs() is a function that returns pairs of a given table, and a for loop iterates through these pairs. Similarly, ipairs() does the same thing except it stops iterating if it hits a nil value. But what about the body of the loop?
A for loop that uses pairs(), ipairs(), or next can be referred to as a generic for loop. It does not use direct numbers, but it can obtain numbers through iterations. In the case you're talking about, you're referring to the standard use case:
for i,v in pairs(table) do
Each variable:
- i is an integer that represents the iteration and position of the iteration.
- v is the value encountered at position i.
Both of these are not hard-locked variables and can be named whatever you want. For instance, if you wanted to iterate through the children of Workspace, you can use different but relevant variable names:
for i, child in pairs(workspace:GetChildren()) do
-- other code
end
child behaves the same as v because it's just a variable name. Variable names, by the way, cannot start with a number, meaning 1apple is not valid, but apple1 is.
How about an example to illustrate usage?
This code uses a dummy table with integers and iterates through it. It prints each value of the table.
local dummy = {1,2,3,4,5}
for iteration, value in pairs(dummy) do
print(value)
end
The output:
1
2
3
4
5
This next code is the same as the previous, except the fourth value is nil and the loop uses ipairs() instead of pairs().
local dummy = {1,2,3,nil,5}
for iteration, value in ipairs(dummy) do
print(value)
end
The output:
1
2
3
ipairs() stopped at the fourth value because it was nil.
These are really the basics of generic for loops. You can expand upon this to greater heights.
A quick google search results in: "for i,v in pairs() do is basically an advanced loop. Generally, this is used to iterate over a table, or I also use it to iterate over members of a Model by using :GetChildren()", so it basically goes through every child in an object, and does/checks something. For example, you could use this to go through the workspace and delete everything that is not terrain, like this:
for i,v in pairs(workspace:GetChildren()) do
if v.Name ~= "Terrain" then
v:Destroy()
end
end
This goes through every child in the workspace, and checks its name, if the name is not "Terrain", then it destroys the object
for i,v in pairs loop is used to go over a table and its values. I really never understood tables, but it is useful to go over children of a part, or get values in a datastore. What I personally use this the most for is a weld script, which will be provided.
--[Place this script under a part in workspace. That's all. Everything will be welded.]--
for i,v in pairs(script.Parent:GetDescendants()) do
if v:IsA("BasePart") or v:IsA("Part") or v:IsA("MeshPart") then
local WELD = Instance.new("WeldConstraint")
WELD.Parent = v
WELD.Part0 = v
WELD.Part1 = script.Parent
print(i.." weld(s) finished")
end
end
This weld script is open source, you can take it here.
This discussion is preserved as it was. New answers, comments, and votes are closed.
