What is the difference on "pairs", "ipairs" and "next"? The ROBLOX Wiki is not precise on this subject, someone could explain me the difference and where to use them?
for index, value in pairs(game.Players:GetChildren()) do print(index, value) end for index, value in ipairs(game.Players:GetChildren()) do print(index, value) end for index, value in next, game.Players:GetChildren() do print(index, value) end
pairs
returns next, t, nil
. t
being the table passed to pairs
, nil
being the first key to next
. They are kinda the same thing, so I won't go into detail about this.
They're similar, but ipairs
is a bit different. When a nil
value is reached, it stops, as pellm said in the comments. Take this for example:
local array = {"hello", "how", nil, "you"} for _, v in ipairs(array) do print(v) end
Should print:
hello
how
Because nil
was reached, it didn't print nil
, and "you" wasn't even reached.