getrawmetatable

Gets the raw metatable of an object, bypassing __metatable.

Syntax

getrawmetatable(object: any) -> table?

Also available as debug.getmetatable.

Parameters

ParameterTypeDescription
objectanyThe object to get the metatable from

Returns

TypeDescription
table?The raw metatable, or nil if none

Description

getrawmetatable retrieves the actual metatable of an object, bypassing the __metatable field that would normally prevent access via getmetatable.

Example

-- Normal getmetatable is blocked on game objects
print(getmetatable(game)) -- "The metatable is locked"

-- getrawmetatable bypasses this
local mt = getrawmetatable(game)
print(mt) -- table: 0x...
print(mt.__index) -- function
print(mt.__namecall) -- function

Inspecting Metamethods

local mt = getrawmetatable(game)

for key, value in pairs(mt) do
    print(key, type(value))
end
--[[
__index      function
__newindex   function
__namecall   function
__tostring   function
...
]]

Before Hooking

-- Always get the metatable before modifying
local mt = getrawmetatable(game)
local oldIndex = mt.__index

-- You may need to unlock it first
setreadonly(mt, false)
mt.__index = newcclosure(function(self, key)
    print("Indexing:", key)
    return oldIndex(self, key)
end)
setreadonly(mt, true)