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
| Parameter | Type | Description |
|---|---|---|
target | function | The function to replace. |
hook | function | The function that runs instead. |
Returns
| Type | Description |
|---|---|
function | A 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
- Inside the hook, call the returned original, not
target; callingtargetwould run the hook again. - Use
checkcallerinside the hook if your own calls should get the original behavior. isfunchookedtells you whether a function is currently hooked.
These are Game executor contracts. Check build requirements and session lifetimes before using them.
