> ## Documentation Index
> Fetch the complete documentation index at: https://docsuncv2.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# clonefunction

Returns a copy of `func` that runs the same code, but is a separate function: `clone == func` is always `false`.

```luau theme={null}
clonefunction(func: function): function
```

Works on Lclosures/CClosures. The bytecode is identical, so `getfunctionhash(clone) == getfunctionhash(func)`.

Mainly used to grab a clean copy of a function before some other script hooks it. <br /> The clone keeps the original behavior, so calls through it bypass the hook entirely:

```luau theme={null}
local cleanFunction = clonefunction(someFunction)
-- another script may hookfunction(someFunction, ...) later,
-- cleanFunction still calls the original.
```

## Parameters

| Parameter | Type       | Description            |
| --------- | ---------- | ---------------------- |
| `func`    | `function` | The function to clone. |

## Returns

`function` - same code as `func`, with its own memory address. <br /> Each call to `clonefunction` returns a new function - clone the same one 10 times, and you get 10 separate copies of it.

## Example

<CodeGroup>
  ```luau Example 1 theme={null}
  local Old = clonefunction(print)
  Old = hookfunction(print, function(...)
  	if not checkcaller() then
  		Old(...)
  	end
  	warn(...)
  end)

  print("Hello, World!")
  ```

  ```luau Example 2 theme={null}
  local cloned_function = clonefunction(print)
  local function loop()
  	while task.wait() do
  		if print ~= cloned_function then
  			restorefunction(cloned_function)
  			print("sad")

  			break
  		end
  	end
  end
  task.spawn(loop)

  local old; old = hookfunction(print, function(...)
  	warn(...)
  end)
  ```

  ```text Example 3 theme={null}
  local Lua_Closure = newlclosure(function()
  	return "Hello, World!"
  end)

  local cloned_function = clonefunction(Lua_Closure)
  if dumpbytecode(Lua_Closure) == dumpbytecode(cloned_function) then
  	print(cloned_function)
  end
  ```
</CodeGroup>

<Tip>
  Always check parameters and return values when working with this library function.
</Tip>
