hookfunction

Replaces a function's behavior.

Syntax

Signature
hookfunction(Target: function, Replacement: function) -> function

Aliases

  • replaceclosure
  • hookfunc

Parameters

ParameterTypeDescription
TargetfunctionOriginal function to inspect or replace.
ReplacementfunctionFunction used instead of the target.

Returns

TypeDescription
functionCallable copy of the target's behavior before this hook.

Notes

Returns a separate function for Target's behavior before this call. Hooking the same target again changes its replacement. restorefunction restores the behavior saved by the first hook.

Example

Luau
local Function = function() return "original" end
local Original = hookfunction(Function, function() return "hooked" end)
print(Function(), Original())
restorefunction(Function)