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

# hookfunction

> Hook một target function bằng replacement function và trả về cloned copy của original target closure. Cả hai arguments phải là functions. Function trước tiên flush pending C function swaps, cloning target qua function cloning, thêm replacement closure hoặc mapped newcclosure target của nó vào hidden stack tracking, gọi FixCallstack cho target và cloned original, rồi patch target closure qua C closure, Luau closure hoặc newcclosure-specific path. Nếu target chưa có trong hook records, cloned original được lưu ở đó cho restorefunction. Returned function là cloned original closure.

<div className="real-function-page">
  ```lua theme={null}
  hookfunction(target: function, replacement: function): function
  ```

  ## Alias

  * `hookfunc`
  * `replaceclosure`
  * `detourfunction`
  * `detour_function`

  ## Đối số

  <ParamField path={"target"} type={"function"} required>
    Function cần patch. Truyền non-function sẽ tạo type error.
  </ParamField>

  <ParamField path={"replacement"} type={"function"} required>
    Function có function được install hoặc map lên target. Truyền non-function sẽ tạo type error.
  </ParamField>

  ## Giá trị trả về

  <ResponseField name={"original"} type={"function"}>
    Cloned copy của original target closure được tạo trước khi hook được install.
  </ResponseField>

  ## Ví dụ

  Patch a function closure and keep a cloned original.

  ```lua theme={null}
  local function target(value)
      return value + 1
  end

  local original
  original = hookfunction(target, function(value)
      return original(value) * 2
  end)
  ```
</div>
