hookmetamethod Synchronous

Replace a callable metamethod on an object and return its preceding implementation. An optional fifth argument filters calls before they reach the replacement.

Syntax
Luau
hookmetamethod(
    object: any,
    name: string,
    replacement: Function,
    argguard: boolean?,
    filter: HookFilter?
) -> Function

Parameters

Function parameters
ParameterTypeDescription
objectanyObject whose metatable contains the target metamethod.
namestringMetamethod name, such as "__call".
replacementFunctionFunction called when the filter matches.
argguardboolean?Optional boolean. Defaults to true; false disables Kawaii’s minimum-argument guard for supported native userdata and vector C metamethods.
filterHookFilter?Optional HookFilter in the fifth position. Pass nil for argguard when keeping its default.

Returns

Function

A callable for the preceding metamethod implementation.

Usage notes

ArgGuard does not apply to user tables and Luau metamethods. On supported native C metamethods it bypasses the replacement for calls missing required receiver, arguments, or namecall context; it does not type-check extra keys or method arguments.

A filter miss calls the preceding metamethod. Omit the filter or pass nil to handle every eligible call.

Example

Example
Luau
local object = setmetatable({}, {
    __call = function(_, value) return value * 2 end,
})
local original = getmetatable(object).__call
local previous
previous = hookmetamethod(object, "__call", function(self, value)
    return previous(self, value) + 1
end, false, ArgumentFilter.new(2, 3))
print(object(3)) -- 7
print(object(4)) -- 8
restorefunction(original)
Kawaii documentation