Yuari
Documentation
Docs/Debug/debug.setmetatable

debug.setmetatable

Sets an object metatable and returns the object.

Syntax

debug.setmetatable(object: any, metatable?: table) -> any

Parameters

ParameterTypeDescription
objectanyThe object value used by the call.
metatable?tableThe metatable value used by the call.

Returns

TypeDescription
anyThe 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.