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
| Parameter | Type | Description |
|---|---|---|
object | any | The value whose metatable to set. |
metatable | table? | The new metatable, or nil to remove it. |
Returns
| Type | Description |
|---|---|
any | object, 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.
