Volt

hookmetamethod

Hooks a metamethod on an object's metatable.

Syntax

hookmetamethod(object: any, metamethod: string, hook: function) -> function

Parameters

ParameterTypeDescription
objectanyAn object with the target metatable
metamethodstringThe metamethod name (e.g., "**index", "**namecall")
hookfunctionThe replacement function

Returns

TypeDescription
functionA reference to the original metamethod

Description

hookmetamethod replaces a metamethod in an object's metatable with your custom function. This is commonly used to intercept method calls, property accesses, and other metamethod operations on Roblox instances.

Example

-- Hook __namecall to intercept method calls
local oldNamecall
oldNamecall = hookmetamethod(game, "__namecall", newcclosure(function(self, ...)
    local method = getnamecallmethod()

    if method == "GetService" then
        print("GetService called with:", ...)
    end

    return oldNamecall(self, ...)
end))

-- This triggers the hook
local players = game:GetService("Players")

Common Metamethods

MetamethodTriggered By
__indexProperty reads (obj.Property)
__newindexProperty writes (obj.Property = value)
__namecallMethod calls (obj:Method())
__tostringtostring(obj)
__eqEquality comparison (obj1 == obj2)

Notes

  • Automatically uses newcclosure when hooking metamethods on Roblox objects
  • Use getnamecallmethod inside __namecall hooks to get the method name
  • Multiple objects may share the same metatable
  • Use checkcaller to filter volt vs game calls

On this page