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.
boolean
default:"true"
Accepted for compatibility with other executors and ignored. Athena reads no behavior from this argument. Pass it or omit it; the result is the same.
Filter
An optional filter. When given, hook runs only for calls the filter matches; every other call goes straight to the original metamethod. The filter is evaluated natively, so non-matching calls never enter Luau.

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:

Filtering calls

A __namecall hook runs for every method call in the game, which is a lot of Luau on a hot path. Pass a filter and the work happens in C++ instead: calls that do not match never reach your hook.
Filters compose, so you can narrow further — this one matches only FireServer calls on a RemoteEvent:
Note that argument indices count the call’s own arguments, so for a namecall index 1 is the object the method was called on. See Filter.

Errors