I’ve recently gotten into Object-Oriented Programming (OOP) with Roblox and I was wondering if there was a way to reference the base metastable. For example:
-- ModuleScript
local Gun = {}
Gun.__index = Gun
function Gun.new(tool, owner)
local newGun = {}
setmetatable (newGun, Gun)
newGun.Tool = tool
newGun.Owner = owner
end
Gun:GetMetatable()
print(-- Reference to the metatable here)
end
return Gun
-- Is there anyway to do that? ^
Lua has getmetatable(table) function, which returns the metatable of the provided table. You can simply write this:
function Gun:GetMetatable()
print(getmetatable(self))
end
However, if the metatable has __metatable value in it, then the getmetatable function will return that value instead of an actual metatable. This is used for security to prevent overriding the functions. For example, using getmetatable on any instance (which are actually just userdata values with metatables) will return a string "The metatable is locked".
You cannot transfer metatables between client and server.
You don’t need to get the metatable to call the functions, just call them from the table itself.
And you can’t store a table reference in the instance.