Skip to content

hookmetamethod

Notes on hookmetamethod

hookmetamethod can be safely implemented from within Luau, as long as hookfunction is already properly implemented in C++.

hookmetamethod takes any Luau object that can have a metatable, and attempts to hook the specified metamethod of the object. Internally, it essentially uses hookfunction to hook specific metamethods.

function hookmetamethod(object: { any } | userdata, metamethodName: string, hook: (...any) -> (...any)): (...any) -> (...any)

Parameters

Parameter Description
object The object which has a metatable.
metamethodName The name of the metamethod to hook.
hook The function that will be used as a hook.

Example

Easily hooking metamethods with hookmetamethod
1
2
3
4
5
6
7
8
9
local original; original = hookmetamethod(game, "__index", function(...)
    local key = select(2, ...)
    print(key)
    return original(...)
end)

local _ = game.PlaceId -- Output: "PlaceId"

hookmetamethod(game, "__index", original) -- Restores game's __index