> ## 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.

# hookmetamethod

> Tìm metamethod function trên một value và hook function đó thông qua hookfunction. First argument có thể là bất kỳ value nào được raw metafield lookup chấp nhận. Second argument phải là string metamethod name, và third argument phải là function replacement. Nếu raw metafield lookup không tìm thấy named metafield, function tạo "Metamethod doesn't exist" cho argument 2. Nếu metafield tồn tại nhưng không phải function, function tạo "Metamethod isn't a function" cho argument 2. Khi metafield là function, hookmetamethod gọi hookfunction với found metamethod function làm target và trả về cloned original closure của hookfunction.

<div className="real-function-page">
  ```lua theme={null}
  hookmetamethod(object: any, metamethod: string, replacement: function): function
  ```

  ## Đối số

  <ParamField path={"object"} type={"any"} required>
    Value có methods được kiểm tra bằng raw metafield lookup.
  </ParamField>

  <ParamField path={"metamethod"} type={"string"} required>
    Metafield name được truyền vào raw metafield lookup.
  </ParamField>

  <ParamField path={"replacement"} type={"function"} required>
    Function được truyền vào hookfunction làm replacement cho found metamethod function.
  </ParamField>

  ## Giá trị trả về

  <ResponseField name={"original"} type={"function"}>
    Cloned original metamethod closure được hookfunction trả về.
  </ResponseField>

  ## Ví dụ

  Hook an existing metamethod function.

  ```lua theme={null}
  local object = setmetatable({}, {
      __index = function(_, key)
          return key
      end,
  })

  local original
  original = hookmetamethod(object, "__index", function(self, key)
      if key == "Name" then
          return "Real"
      end

      return original(self, key)
  end)
  ```
</div>
