How can I make metatables work with next()?

So, I was using metatables to detect changes in player data and for some reason iterating through a metatable like

for i, v in MetaTable do
 -- do stuff
end

works but not

print(next(MetaTable))

Using next() would help cut down on a lot of lines of code and make things easier to read.
(Here’s my __iter function)

__iter = function()
	return next, Copy
end

Is there a way to make next() work with metatables?

next() works directly with the table, ignoring __iter. Custom iteration works in for loops via __iter, but next() won’t use it. Example:

local Copy = {a = 1, b = 2, c = 3}

local MetaTable = {
    __iter = function(t)
        return next, t, nil
    end
}

setmetatable(Copy, MetaTable)

for k, v in Copy do -- Uses __iter
    print(k, v)
end

print(next(Copy)) -- Ignores __iter

So I suppose I should just make a custom function like :GetOriginalTable() that well, returns the original table so I can use the next() function thanks.

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