Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

which is better to use here, pairs or ipairs() ??

Asked by 3 years ago
local VotingUIs = {
    script.Parent:WaitForChild("Classic"),
    script.Parent:WaitForChild("Combat"),
    script.Parent:WaitForChild("Double Killers")
}
for i, v in ipairs(VotingUIs) do
    print(v)
end

using both pairs and ipairs work here, but i want to know which is better to use in this situation.

even though the forum says that ipairs() is for arrays, pairs() seem to work for arrays as well

1
they would be the same for that i think. pretty sure ipairs and pairs are the same but ipairs stops when something is nil https://scriptinghelpers.org/questions/2725/difference-between-pairs-ipairs-and-next BulletproofVast 1033 — 3y

1 answer

Log in to vote
1
Answered by 3 years ago

Truthfully, it boils down to personal choice here. Both in pairs and in ipairs will work for an array, but only in pairs works for dictionaries.

If you want something faster you could use a for i = 1,n loop and index the table using that like so:

local table = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }

for i = 1, #table, 1 do
    print(table[i])
end

Again, they accomplish the same thing it all boils down to aesthetic.

Ad

Answer this question