for index, value in table do
end
for index, value in pairs(table) do
end
for index, value in ipairs(table) do
end
for index, value in table do
end
for index, value in pairs(table) do
end
for index, value in ipairs(table) do
end
pairs and ipairs are iterators that traverse pairs of indices/keys and the values they point to. The difference between them is that, compared to pairs, ipairs is ordered and intended to be used with sequential arrays (indices 1…n). the i stands for ‘index’.
Best to read a quick article with examples about them: pairs and ipairs | Documentation - Roblox Creator Hub.
The first option is generalised iteration which eliminates the need to distinguish the two iterators but keeps similar behaviour, so it has become the new standard in luau.
So the first option, I’m assuming is the best one to use? As it is clearly shorter and apparently does the same things
That’s right. Roblox has also mostly removed pairs/ipairs from the code samples in the docs. You can of course still use them if you’d like. They will definitely stay indefinitely.
Generalised iteration is quite similar, but on tables it performs the roles of both. In mixed tables it behaves like ipairs would for consecutive indices (1…n). ipairs would stop at the ‘gap’ or at the ‘end’, whereas in this case the iterator then proceeds like pairs would and visits all keys in no particular order.
Example:
local mixed = {
[1] = "a";
[2] = "b";
[15] = "c";
[150] = "d";
[151] = "e";
f = "f";
g = "g";
}
for k, v in mixed do
print(k, v)
end
The best summary I can give is that:
Option 1 = Option 2 (these are good for most use cases)
Option 3 is good for arrays (tables with numerical indices) because it offers a performance increase. It does not work in other cases.