--- title: Metatables description: Metatables attach powerful metamethods to tables, allowing for manipulation like indexing, addition, and concatenation. --- Metatables allow tables to become more powerful than before. They are attached to data and contain values called metamethods. Metamethods are fired when a certain action is used with the datum that it is attached to. ## Manipulate metatables The two primary functions for adding and finding a table's metatable are `Global.LuaGlobals.setmetatable()` and `Global.LuaGlobals.getmetatable()`. ```lua local x = {} local metaTable = {} -- metaTables are tables, too! setmetatable(x, metaTable) -- Give x a metatable called metaTable! print(getmetatable(x)) --> table: [hexadecimal memory address] ``` The `Global.LuaGlobals.setmetatable()` function also returns the table that you're setting the metatable of, so these two scripts do the same thing: ```lua local x = {} setmetatable(x, {}) ``` ```lua local x = setmetatable({}, {}) ``` ### Metamethods Metamethods are the functions that are stored inside a metatable. They can go from calling a table, to adding a table, to even dividing tables as well. Here's the list of available metamethods:
| Method | Description |
|---|---|
| `__index(table, index)` | Fires when `table[index]` is indexed, if `table[index]` is `nil`. Can also be set to a table, in which case that table will be indexed. |
| `__newindex(table, index, value)` | Fires when `table[index]` tries to be set `(table[index] = value)`, if `table[index]` is `nil`. Can also be set to a table, in which case that table will be indexed. |
| `__call(table, ...)` | Fires when the table is called like a function, `...` is the arguments that were passed. |
| `__concat(table, value)` | Fires when the `..` concatenation operator is used on the table. |
| `__unm(table)` | Fires when the unary `–` operator is used on the table. |
| `__add(table, value)` | The `+` addition operator. |
| `__sub(table, value)` | The `–` subtraction operator. |
| `__mul(table, value)` | The `*` multiplication operator. |
| `__div(table, value)` | The `/` division operator. |
| `__idiv(table, value)` | The `//` floor division operator. |
| `__mod(table, value)` | The `%` modulus operator. |
| `__pow(table, value)` | The `^` exponentiation operator. |
| `__tostring(table)` | Fired when tostring is called on the table. |
| `__metatable` | If present, locks the metatable so `Global.LuaGlobals.getmetatable()` will return this instead of the metatable and `Global.LuaGlobals.setmetatable()` will error. Non-function value. |
| `__eq(table, value)` | The `==` equal to operator¹ |
| `__lt(table, value)` | The `<` less than operator¹ |
| `__le(table, value)` | The `<=` operator¹ |
| `__mode` | Used in weak tables, declaring whether the keys and/or values of a table are weak. Note that references to Roblox instances are never weak. Tables that hold such references will never be garbage collected. |
| `__len(table)` | Fired when the `#` length operator is used on the object. |
| `__iter(table)` | Used to denote a custom iterator when using generalized iteration. |