What is the purpose of pairs()

i have never really understood the purpose of pairs() i know it has to do with for loops but when ever i use a for loop i can just do: for i,v in table do and it works fine i know in older versions of roblox it was needed but does it still have a purpose now?

--Clears everything in workspace
for i,v in pairs(workspace:GetChildren()) do
	v:Destroy()
end
--Also clears everything in workspace
for i,v in workspace:GetChildren() do
	v:Destroy()
end

Ehhhhhh, not really. More just backwards compatibility.

It used to be required for iterating through arrays (ipairs - maintains the order of arrays) and dictionaries (pairs - no order [for non-incrementing indexes]), however it was (somewhat) recently replaced with the generalized for index, value in table do.
For new works, you should be using the generalized one.

1 Like

Both ipairs and pairs iterate through the array part of tables in the same order, it’s just that ipairs only supports the array part, whereas pairs also iterates through the dictionary.

pairs() returns a function (closure) that acts like a generator. It returns different values each time it is called, until it eventually returns nil. Roblox has changed the way for loops work so that they automatically choose a generator for tables, so most of the time pairs and ipairs are not required. One of the main times you still need ipairs is when iterating over a table produced by table.pack, since it has both string and number keys, and you usually only want the numbered ones.

1 Like

For clearer explanation:

Roblox uses a modification of Lua and is called Luau.
In older versions, Luau didn’t have generalised for loops.
Before the update, you’d have to use pairs() and other iterator functions, i.e ipairs(), next().

You’ll notice that if you attempt to write a generalised for loop in Lua and not Luau, you’ll get an error. Generalised for loops are exclusive to Luau.

What pairs() does is return a tuple (a, b) in the form key/index, value.

A key is essentially the name given to a value in a dictionary, i.e the hello in {hello = 1}

An index is a number-type key assigned to a value in an array. You can either specify it explicitly (directly setting the indexes) or implicitly (leaving it blank).

local explicitIndexes = {
        [1] = "nice",
        [2] = "wow",
        [3] = "fantastique"
    }

--explicitIndexes[1] = "nice"
--explicitIndexes[2] = "wow"
--explicitIndexes[3] = "fantastique"

local implicitIndexes = {"value", "hello", 9}
--implicitIndexes[1] = "value"
--implicitIndexes[2] = "hello"
--implicitIndexes[3] = 9

The value is what is given as an equals operation, i.e the 1 in {hello = 1}

In short, pairs() returns a key/index-value pair.
ipairs() (short for indexpairs) on the other hand returns an index-value pair. If you give a table with a string key to ipairs(), it will error.

This is also why it says in.
in pairs() → in pairs
in ipairs() → in index pairs

If you decide to use generalised for loops, it will default to using `pairs()’.

Hope this helps!

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.