ReferenceClosures

hookfunction

Replaces a function in place, so every existing reference to it runs your hook instead.

Syntax

hookfunction(target: function, hook: function) -> function

Also available as

hookfuncreplaceclosurehook_funchook_functionreplace_closure

Parameters

ParameterTypeDescription
targetfunctionThe function to replace.
hookfunctionThe function that runs instead.

Returns

TypeDescription
functionA callable copy of the original target, unaffected by the hook.

Description

Unlike assigning a new value to a variable or table field, hookfunction changes the function object itself. References the game already holds, upvalues that captured it and tables that store it all call hook from then on. The return value is a copy of the original that you keep to call the real behavior from inside the hook.

All combinations of Luau and C functions are supported in both positions, including wrappers from newcclosure. To undo the hook, call restorefunction on the target.

Example

local function damage(amount)
    return amount
end

local original
original = hookfunction(damage, function(amount)
    return original(amount) * 2
end)

print(damage(10))
restorefunction(damage)
print(damage(10))

Notes

These are Game executor contracts. Check build requirements and session lifetimes before using them.