Metatable help to create custom player table

Hello,

I wanted some feedback about indexing and metatables, what would be the most efficient to have my own player table (with the player instance included). I have 2 ideas, which one would be the most optimized and why ?

First Idea :

 -- downside maybe too intensive, need to write all the fcts and events, can't parent instances to the player
local RealPlrFct = {
    ClearCharacterAppearance = true;
    DistanceFromCharacter = true;
    GetFriendsOnline = true;
    GetJoinData = true;
    GetMouse = true;
    ...
}

local RealPlrEvent = {
    CharacterAdded = true;
    CharacterAppearanceLoaded = true;
    ...
}

function Player.new(plr)
    local self = setmetatable({}, {
        __index = function(_, v)
            if Player[v] then
                return Player[v]
            elseif RealPlrFct[v] then
                return function(_, ...)
                    return plr[v](plr, ...)
                end
            elseif RealPlrEvent[v] then
                return plr[v]
            end
        end
    })

    for i,v in pairs(self:GetChildren()) do
        self[i] = v
    end

    self.ChildAdded:Connect(function(Child)
        self[Child.Name] = Child
    end)

    self.ChildRemoved:Connect(function(Child)
        self[Child.Name] = nil
    end)

    return self
end
-- but it allows me to add pretty much whatever I want to the player table, and I don't need to do plr.Instance

Second Idea :

-- downside I always have to do plr.Instance if I want to get the fcts, events, ...
function Player.new(plr)
    local self = setmetatable({Instance = plr}, Player)

    return self
end
-- but I guess it'd be less intensive ?
Most opti ?
  • First Idea
  • Second Idea

0 voters

1 Like