ReferenceMetatables

setrawmetatable

Sets a value's metatable, even when the current one is protected by __metatable.

Syntax

setrawmetatable(object: any, metatable: table?) -> any

Also available as

debug.setmetatableset_raw_metatable

Parameters

ParameterTypeDescription
objectanyThe value whose metatable to set.
metatabletable?The new metatable, or nil to remove it.

Returns

TypeDescription
anyobject, so the call can be chained.

Description

setmetatable refuses to replace a protected metatable and only works on tables. setrawmetatable replaces it in every case. A frozen table keeps its frozen state when its metatable changes. For userdata such as Instances, this replaces that reference's metatable; editing the contents of a shared metatable can affect other references. Primitive types such as strings use a type-wide metatable, so restore their original afterwards.

Example

local t = setmetatable({}, { __metatable = "locked" })
setrawmetatable(t, { __index = function(_, key) return key .. "!" end })
print(t.hello)

These are Game executor contracts. Check build requirements and session lifetimes before using them.