What's the difference between ipairs() and in pairs()?

I just wondered what`s the difference of this two, for me they are both the same!

2 Likes

Pairs is used to iterate over dictionaries, while ipairs is used to iterate over arrays. Both of them are obsolete now though, because Roblox added generalized iteration which is way faster.

Code:

for i, child in workspace:GetChildren() do

end
7 Likes

ipairs gets index and value, pairs gets key and value

2 Likes

While we do use “index” and “key” to refer to different ways of keying a table, that’s only a difference in nomenclature but that doesn’t really explain much.

To be a bit clearer: ipairs will iterate a table assuming it is numerically contiguously keyed, whereas pairs calls next (or, if that behaviour has changed, something similar to the same instructions).

I would just use generalised iteration though, as Katrist mentioned. Not only is it faster, but it also does both jobs; it will iterate numerically contiguous elements before iterating in an undefined order. See: Generalized iteration on Luau Lang

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.