I don't quite understand what ipairs does. Help?

Hello, I’m currently a developer that’s learning a lot of new things such as OOP and some other stuff, but recently I have heard about ipairs on Roblox, I’ve watched a lot of stuff about it but still don’t quite understand it, May anyone explain it to me? Thank you -MattVSNNL

2 Likes

So it’s basically like GetOrderedDataStores where it ascends or descends from highest or lowest? In ipairs case only descends

Nothing to do with OO.
In Lua you have the data structure table, a table has keys and values. Keys could be either a number or a string. If the numbers are consistent and start with the lowest index possible (in Lua 1) - the table is acting like the data structure array.
ipairs allows you to loop through that array. If you have keys that are not consistent nor numbers - it will not loop through them. For that we use pairs.
Personally, I don’t use ipairs at all lol

2 Likes

As @rotbotrotbot explained, tables have two structures: dictionaries and arrays.
Any table that is not an array behaves as a dictionary. For a table to be an array it must start with index 1 and not have any missing value in between indeces, and must be consistent keys (only numbers)

Ipairs will follow the array rule, in order, until a value is nil, that’s why you shouldn’t use ipairs for any other thing than arrays.

You can imagine ipairs as this:

local tab = {}
for i = 1, #tab, 1 do
      local val = tab[i]
      if val == nil then
      break
      end
end

Whereas pairs will go through every item in the table, but not in order

I generally never use ipairs because of what I mentioned, it stops, if you generally need ordering just use a for i loop