Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
3

What does 'i,v in pairs do' mean?

Asked by 9 years ago

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?

0
index and value used to edit / access a table User#5423 17 — 9y

1 answer

Log in to vote
2
Answered by
Relatch 550 Moderation Voter
9 years ago

"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!

0
Ok that helps a little, but what is pairs used for? ghostblocks 74 — 9y
0
Pairs is basically "i and v". Relatch 550 — 9y
0
Alright thanks, accepting the answer! :D ghostblocks 74 — 9y
0
Can you thumbs up too? Relatch 550 — 9y
View all comments (3 more)
0
he cant thumbs up -_- davness 376 — 9y
0
wow who thumbs down me Relatch 550 — 9y
0
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) iaz3 190 — 9y
Ad

Answer this question