local ents = {
GetLocalPlayer = function()
local tbl = {
localplayer = {"Ava", "1", {213,234,234}},
GetIndex = function(self)
return self.localplayer[2]
end,
}
setmetatable(tbl, getmetatable(tbl.localplayer))
return tbl
end
}
local function main()
print(ents.GetLocalPlayer()[2])
end
main()
print returns nil. If I was to do ents.GetLocalPlayer():GetIndex()
however, it returns 1.
The idea is to have the default return value to be localplayer if I don't do things such as GetIndex()
A table has no default metatable, which is why your getmetatable
call returns nil. In order to do anything, the second argument to setmetatable
must be a table that has at least one metamethod. (__index
is the most common metamethod.)
The solution is to change getmetatable(tbl.localplayer)
to {__index = tbl.localplayer}
.