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

Difference between "pairs" and "ipairs"?

So i very recently learned the for i,v in pairs loop and ive seen some people doing "for i,v in ipairs" instead of "for i,v in pairs"? What's the difference?

1 answer

✓ Accepted answer

So basically for i,v in pairs will go through an entire table, and do whatever it's asked to. But for i,v in ipairs has a twist. It stops when it reaches a nil value. For example:

local myTable = {1,2,nil,3}
for i,v in pairs(myTable) do
	print(v)
end

This would output:

1
2
3

But if we did:

local myTable = {1,2,nil,3}
for i,v in ipairs(myTable) do
	print(v)
end

Would only output:

1
2

Because it stops at the nil value

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