Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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

3 comments

1

pairs could be compared to next, as pairs returns next

1

In a sense they're kind of the same thing

2

ipairs stops at nil

1 answer

#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.

2 comments

0

Okay and i use it where?

0

For ipairs

This discussion is preserved as it was. New answers, comments, and votes are closed.