debug.setmetatable
Sets an object metatable and returns the object.
Syntax
debug.setmetatable(object: any, metatable?: table) -> any
Parameters
| Parameter | Type | Description |
|---|---|---|
object | any | The object value used by the call. |
metatable? | table | The metatable value used by the call. |
Returns
| Type | Description |
|---|---|
any | The object |
Description
debug.setmetatable is part of the Debug library. The debug library exposes the internals of the active Luau state, including protos, constants, upvalues and stack frames.
The change is applied directly to the live target, so later reads observe the updated value.
The aliases setmetatable(object, metatable?) resolve to the same implementation.
Example
local object = {}
debug.setmetatable(object, { __index = function() return 1 end })
print(object.anything)
Notes
- Aliases:
setmetatable(object, metatable?). - Writing protos, constants or upvalues can corrupt a running function; test changes carefully.