clonefunction
Creates an independent copy of a function.
Syntax
clonefunction(fn: function) -> function
Parameters
| Parameter | Type | Description |
|---|---|---|
fn | function | The function to clone. |
Returns
| Type | Description |
|---|---|
function | A copy of the original function. |
Description
clonefunction creates an independent Luau closure that still executes the same prototype as the original function.
The clone shares the original upvalues, so captured values remain connected, but hooks installed on the original do not redirect calls made through the clone. This makes it useful for preserving the pre-hook behavior of a function before replacing it with hookfunction.
Example
-- Clone a function before hooking it
local originalPrint = clonefunction(print)
-- Now hook the original
hookfunction(print, function(...)
originalPrint("[HOOKED]", ...)
end)
-- The clone still calls the original behavior
originalPrint("This bypasses the hook")
print("This goes through the hook")
Notes
- Closure operations act on live Luau function objects; installed hooks remain active until restored.
- Cloned functions share the same upvalues as the original.
- C closures are supported.
- Cloning is useful for preserving original behavior before hooking.