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

(Built-in-Functions) What is the difference on "pairs", "ipairs" and "next" and where to use them?

Asked by 5 years ago

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
1
pairs could be compared to next, as pairs returns next User#19524 175 — 5y
1
In a sense they're kind of the same thing User#19524 175 — 5y
2
ipairs stops at nil piRadians 297 — 5y

1 answer

Log in to vote
2
Answered by 5 years ago

pairs vs next

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.

pairs vs ipairs

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.

0
Okay and i use it where? NiniBlackJackQc 1562 — 5y
0
For ipairs NiniBlackJackQc 1562 — 5y
Ad

Answer this question