Hooks one metamethod of object’s metatable, redirecting it to hook. Returns a callable for the original metamethod so you can forward to it. This is the targeted, safe way to intercept behavior like __index, __namecall, or __newindex on a specific object’s metatable.

Parameters

any
required
The value whose metatable carries the metamethod. It must have a metatable.
string
required
The metamethod name, e.g. "__index", "__namecall", "__newindex".
function
required
The replacement metamethod.

Returns

function
A function that invokes the original metamethod. Call it to forward.

Behavior

  • Readonly metatables are handled. Game metatables are typically readonly; hookmetamethod records what it changed so restoremetamethod and restorefunction can both undo it, without leaving the metatable writable.
  • The metamethod must already be a function. Hooking a metamethod that is not a function (or absent) is rejected.
  • Pair with checkcaller. Inside the hook, use checkcaller to separate the game’s calls from your own and forward the rest unchanged.

Example

Intercept remote calls via __namecall:

Errors