Metatables

getrawmetatable

Returns the metatable currently attached to a target value, or nil when none is present.

Syntax

getrawmetatable(target: any): table | nil
Aliases debug.getmetatable

Arguments

NameTypeDescription
targetanyValue whose metatable is returned.

Returns

NameTypeDescription
metatabletable | nilTarget metatable when present; otherwise nil.

Description

Returns the metatable currently attached to a target value, or nil when none is present. The function returns exactly one value and does not change the target. debug.getmethods is an alias for getrawmethods.

Call it with 1 parameter(s): target. The argument table explains which values are required and which ones only refine the behavior.

It returns metatable (table | nil). Use the returns table to separate successful values from nil results and recoverable errors.

Example

Return the metatable currently attached to a value.

local target = {}
local meta = {
    label = "Real",
}

setrawmetatable(target, meta)

print(getrawmetatable(target) == meta)
print(debug.getmetatable(target) == meta)