Skip to content
Functions & closuresReference

hookfunction

Represents interception of function behavior.

Aliaseshookfuncreplaceclosure

Details#

A hook replaces the behavior of a function value in the current session. Existing references to that value also call the replacement. Save the returned previous implementation and call that value inside the replacement; calling target there would recurse. Repeated hooks return the preceding replacement. restorefunction removes the chain and restores the initial implementation. hookfunc and replaceclosure are aliases.

Signature#

Luau
hookfunction(target: function, replacement: function): function

Parameters#

ParameterTypeDescription
targetfunctionThe exact function value to inspect or modify.
replacementfunctionReplacement callable, receiving the target's arguments.

Returns#

The previous callable implementation. Both arguments must be functions.

Examples#

Example

Luau
local function add(a, b) return a + b end
local saved = add
local original
original = hookfunction(add, function(a, b)
    return original(a, b) * 2
end)
print(saved(2, 3)) -- 10
print(restorefunction(add)) -- true
print(add(2, 3)) -- 5