I've seen some scripts using a i,v in pairs do loop and looking at the wiki wasn't really clear, could someone give an example as to what exactly it does? Like compared to a i=1,#tableName do loop, what is the difference between them?
What does 'i,v in pairs do' mean?
1 answer
"for i, v in pairs" is a for loop. You can use it to search through different things to find something. It also has a perimeter on it. So, say you're searching through workspace for something. Your perimeter would be "(workspace:GetChildren())". I know, this may seem hard to understand, but here it is in script form.
In this script, I made a hat remover. "While wait() do" is the loop. Then, you use the for loop to search through workspace. After that, it uses "IsA" to check if "v" is a hat. v is everything in workspace. What I mean by that is you use "v in pairs(workspace:GetChildren()) do", which makes v the children of workspace. Then finally, the script destroys v if it's a hat.
while wait() do
for i, v i pairs(workspace:GetChildren()) do
if v:IsA("Hat") then
v:Destroy()
end
end
end
"for i, v in pairs" isn't only used for this, it can be helpful along the way. You can make a disco floor, disappearing plates, and many other things.
If this helped, make sure to accept this as your answer and upvote me!
7 comments
Ok that helps a little, but what is pairs used for?
Pairs is basically "i and v".
Alright thanks, accepting the answer! :D
Can you thumbs up too?
he cant thumbs up -_-
wow who thumbs down me
FYI, IsA shouldent be used for classes like that. it should check higher up in the hierarchy(http://wiki.roblox.com/index.php?title=Object_Hierarchy)
This discussion is preserved as it was. New answers, comments, and votes are closed.

1 comment
index and value used to edit / access a table