Yuari
Documentation
Docs/Closures/hookfunction

hookfunction

Redirects a function and returns a callable original. An optional filter decides which calls reach the replacement. All three names are aliases.

Syntax

hookfunction(target: function, hook: function, filter?: FilterBase) -> function

Parameters

ParameterTypeDescription
targetfunctionThe function whose calls should be redirected.
hookfunctionThe replacement function invoked in place of target.
filter?FilterBaseOptional filter that decides whether a call reaches the replacement or the original.

Returns

TypeDescription
functionThe original function, or nil if the hook could not be installed.

Description

hookfunction redirects future calls to the target through the replacement function and returns a callable original.

The returned original lets you forward calls to the previous implementation. When a filter is supplied, matching calls reach the replacement while other calls continue to the original behavior. The hook remains installed until restorefunction is called for that function.

Example

local function add(a, b) return a + b end
local original = hookfunction(add, function(a, b)
    return original(a, b) * 10
end)
print(add(2, 3))

Notes

  • Aliases: hookfunc, replaceclosure.
  • Closure operations act on live Luau function objects; installed hooks remain active until restored.
  • Use clonefunction if you need an independent copy before installing the hook.
  • Filters are created with the filter library and are re-evaluated on every call.
  • Hooks applied to executor functions can be restored with restorefunction.