Hi there, I’ve seen many types of in pairs, such as i, v, etc. But this one stood out to me since I’ve seen it nearly everywhere but can’t seem to find the documentation for it? What is _, in pairs
?
for i,v in pairs(arg)
makes it possible to perform a function for every items of table.
etc:
local SomeTable = {
["GameName"] = "Simulator",
["Description"] = "Play this game now!",
["Visits"] = 100000,
["Likes"] = 1000,
["Dislikes"] = 10}
for i,v in pairs(SomeTable) do
print(i .. " value is " ..v)
end
Output:
GameName value is Simulator
Description value is Play this game now!
Visits value is 100000,
Likes value is 1000
Dislikes value is 10
What if you iterating inside a folder? Say you wanted to print each object in a folder?
local ChildrenOfFolder = folder:GetChildren()
the “ChildrenOfFolder” value is table of all folder’s children
Script:
for i,v in pairs(ChildrenOfFolder) do
print(i .. " value is " ..v)
end
I see, what is the i and v?
i is index (etc. GameName or 1)
v is item of table
30
Index and value, they can be renammed to anything you want (including _,v).
Index in arrays is basically the member index, however its called keys in dictionaries for loop (which is why you see k,v)
And value is basically the member value
So if I wanted to print a tool name out, I’d do?
for i,v in pairs(folder) do print(v.Name)
As people have said here, “i” and “v” are keys and values, and you can name them whatever you want. The reason you have likely seen “_” in place of “i” is it’s a naming convention that people use to signal that they won’t be using the index in any way in the loop. At least that’s how I use it.
Or you could just not use it, right?
yyyyyyyyyyyeeeeeeeesssssssssss yes of course
I see. I get it now, thank you guys for your support.
This has been solved but just adding on for clarification, pairs() does not require two variables, one is fine.
Example:
for I in pairs(workspace:GetChildren()) do
print(I)
end
Simply prints the numerical index for each child. Arguably not very useful, but still possible.
Interesting, I see.