Trying to make my own custom methods for objects

Hi, I’m trying to make sort of custom methods, like :Clone() and :GetChildren(), so it works fine when it’s a table I’m making a method for, but it doesn’t work when I try to make a method for an object.

local Table = {5, 1, 3}

function Table:Print(Index)
    print(self[Index])
end

Table:Print(1)

This worked just fine, however it doesn’t work when I try to do the same thing with an object.

local Plate = game.Workspace.Baseplate

function Plate:Print()
    print(self.Name)
end

Plate:Print()

Is there a way to make your own methods for objects, and not just tables you make?

No there is not. You will have to wrap the instance.

local function wrap(instance)
    return setmetatable({ this = instance }, { __index = instance })
end

local wrapped = wrap(game.Workspace.Baseplate)

function wrapped:Print()
    print(self.this)
end

wrapped:Print()
1 Like

talking about something like this ?

Your current example won’t work because when you do
function Plate:Print(), you’re attempting to create a key ‘Print’ associated with a function Print(Plate) (Plate.Print = function(self, arg)) inside of an object Plate, try it with a table and it’ll work.

For your case, I think the above reply would be suitable

1 Like