hookmetamethod

Replaces a metatable field.

Syntax

Signature
hookmetamethod(Object: any, Name: string, Replacement: function) -> any

Aliases

This function has no aliases.

Parameters

ParameterTypeDescription
ObjectanyObject whose metatable or property is accessed.
NamestringName of the requested member.
ReplacementfunctionFunction used instead of the target.

Returns

TypeDescription
anyPrevious metamethod field value. This can be a function or a table.

Notes

The metatable's readonly state is restored afterward. To undo this change, assign the returned value back to that metatable field.

Example

Luau
local Object = setmetatable({}, {__index = function() return "before" end})
local Original = hookmetamethod(Object, "__index", function() return "after" end)
print(Object.Value)
getrawmetatable(Object).__index = Original