hookmetamethod
Replaces a metatable field.
Syntax
Signature
hookmetamethod(Object: any, Name: string, Replacement: function) -> anyAliases
This function has no aliases.
Parameters
| Parameter | Type | Description |
|---|---|---|
Object | any | Object whose metatable or property is accessed. |
Name | string | Name of the requested member. |
Replacement | function | Function used instead of the target. |
Returns
| Type | Description |
|---|---|
any | Previous 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