Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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?

1 comment

0

index and value used to edit / access a table

1 answer

✓ Accepted 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

0

Ok that helps a little, but what is pairs used for?

0

Pairs is basically "i and v".

0

Alright thanks, accepting the answer! :D

0

Can you thumbs up too?

0

he cant thumbs up -_-

0

wow who thumbs down me

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)

This discussion is preserved as it was. New answers, comments, and votes are closed.