How do I combine two of information with Methods not just Variables?

Is there a way to transfer functions/methods to another metatable?

like this one it’s just combine Variables

setmetatable(tableA, { __index = tableB })

I want Variables and Methods Combine into another metatable
image (49)

i hope you guys get it what i mean :face_with_spiral_eyes:
and thank you :heart:

Like this?

local tableB = require(script.b)
local tableA = {}
tableA = setmetatable(tableA, { __index = tableA})
function tableA.new()
	local self = tableB.new()
	return self
end
return tableA

Lemme know if this works.

1 Like

You could try setting __index to a function and combining the two.

local function combineMetatables<ClassA, ClassB>(ClassA: ClassA, ClassB: ClassB): (ClassA & ClassB)
    local self = setmetatable({}, {__index = function(_self: ClassA & ClassB, key: any): any?
        if ClassA[key] then
            return ClassA[key]
        elseif ClassB[key] then
            return ClassB[key]
        else
            return nil
        end
    end})
end
1 Like

You can use this Global function.

getmetatable()
2 Likes