Hey, this is more of a vanilla lua question than a roblox lua question, but im wondering, why does the pairs() function return the next function, the passed table, and Nil, especially the third one.
I don’t really understand why pairs() would return nil, especially after trying it the lua demo with just the first 2 returned values of pairs(), and having no issues whatsoever:
It works for dictionaries:


It works for arrays:


And it also works for empty tables


Is there a specific reason why pairs() would return nil, or is it an “empty feature”(idk the correct terminology) of sorts
Thanks for your time 
Using pairs or ipairs returns a key and value. Since you are trying to use it on an empty table, it is returning nil as there are no keys or values inside the table.
That isnt really exactly what i’m asking, im asking why the pairs() function would return nil as a third argument, for example, if I were to add a print(a,b)
neither a nor b would be nil

What do you mean as a third argument?
You haven’t added a third parameter nor would it be useful to have one.
what i mean is that, as the pairs function is basically this:

why would it return nil?
pairs returns nil to represent where to start the iteration from.
pairs returns next function so it acts as an iterator.
pairs returns the table so it knows what to iterate through
for i,v in pairs(t) do
end
Is the same thing as
for i,v in next,t do
end
Not passing nil as the third thing works fine because it is assumed at as nil.
for _ in print,{} do end
--> table: XXXXXX nil
However pairs returning nil as the last value is probably just something with the implementation.
So its likely just some implemenation detail.
print(select("#",pairs{}))
--> 3
Something minor, but that’s not really the full implementation of pairs. (in 5.2+)
There is the __pairs metamethod so you can implement custom behavior with pairs.
local t = setmetatable({},{__pairs=function(...)print(...)return 1 end})
local a,b = pairs(t)
print('\n',a,b)
--> table: 0x1c5bb00
--> 1 nil
So with the taking the __pairs metamethod into account, you can’t really make a lua function which would do the same thing as pairs. (protected metatables)
(similar thing with ipairs and __ipairs)