hookfunction
Replaces a function with a custom implementation.
Syntax
hookfunction(target: function, hook: function) -> functionParameters
| Parameter | Type | Description |
|---|---|---|
target | function | The function to hook |
hook | function | The replacement function |
Returns
| Type | Description |
|---|---|
function | A reference to the original function |
Description
hookfunction replaces the target function with your hook function. All calls to the original function will now go through your hook instead. The function returns a reference to the original, allowing you to call it from within your hook.
Example
-- Hook the print function
local originalPrint = hookfunction(print, function(...)
originalPrint("[PREFIX]", ...)
end)
print("Hello") -- Output: [PREFIX] Hello
-- You can still use the original
originalPrint("Direct call") -- Output: Direct callAdvanced Example
-- Hook a game method
local oldNamecall
oldNamecall = hookfunction(
getrawmetatable(game).__namecall,
newcclosure(function(self, ...)
local method = getnamecallmethod()
if method == "Kick" then
return -- Block kick
end
return oldNamecall(self, ...)
end)
)Notes
- Use
checkcallerto differentiate between your calls and game calls - Automatically wrapps Luau hooks in
newcclosurewhen hooking C closures - The returned original can be called to bypass the hook
- Restore the hook with
restorefunction