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

# setrawmetatable

Sets the raw metatable of `object`, even when `__metatable` is set to lock it.

```luau theme={null}
setrawmetatable(object: any, metatable: table?): any
```

If `object`'s current metatable has `__metatable` set, `setmetatable` errors out. <br /> `setrawmetatable` skips that lock and writes the metatable directly. Pass `nil` for `metatable` to clear `object`'s metatable instead.

Most commonly used to replace the metatable of an Instance whose `__metatable` field would otherwise block a normal `setmetatable` call.

## Parameters

| Parameter   | Type     | Description                           |
| ----------- | -------- | ------------------------------------- |
| `object`    | `any`    | The value to attach the metatable to. |
| `metatable` | `table?` | The metatable to write.               |

## Returns

`any` - the same `object` passed in, by reference. Useful for chaining: `local t = setrawmetatable({}, mt)`.

## Example

<CodeGroup>
  ```luau Example 1 theme={null}
  <<<<<<< HEAD
  setrawmetatable(game, {})
  =======
  -- setrawmetatable(object: any, metatable: table?): any
  >>>>>>> 50e30f53759b0538759af25c9b271514aa2c9290
  ```

  ```luau Example 2 theme={null}
  local t = setmetatable({}, {
    __index = function(self, index)
        print(index)
    end,
    __metatable = "This table is locked."
  })

  print(pcall(setmetatable, t, {})) --> false, cannot change a protected metatable
  print(setrawmetatable(t, {})) --> table: 0x......
  ```
</CodeGroup>

<Tip>
  Bypasses standard Lua restriction checks.
</Tip>
