Skip to content
Saiko

hookmetamethod

Hooks a single metamethod on an object's metatable and returns the original. A shorthand for getrawmetatable plus setreadonly plus hookfunction.

Signature

luau
hookmetamethod<A..., R...>(object: Instance | table, method: string, hook: (A...) -> R...): (A...) -> R...

Parameters

objectInstance | table

The object whose metatable is patched.

methodstring

The metamethod name, such as "__index" or "__namecall".

hook(A...) -> R...

The replacement metamethod.

Returns

function

Example

luau
local old
old = hookmetamethod(game, "__namecall", function(self, ...)
    if getnamecallmethod() == "Kick" and not checkcaller() then
        return
    end
    return old(self, ...)
end)