Metatable

A table attached to another table that changes how it behaves, through keys such as __index and __tostring.

Think of it as A note clipped to a card saying where to look for anything not written on it.

setmetatable(t, mt) attaches mt to t. When code reads a key t lacks, Luau checks mt.__index; other metamethods handle tostring, ==, arithmetic and writes. Luau classes are built this way. Metatables are lost when a table is sent through a remote or saved to a DataStore.

Where the picture breaks

Only reads go to the book. Writes land on the card, and a table kept in the book is one shared table that every object changes.

__index is consulted only when a key is missing on read. Assignments create fields on the object itself. Mutable state, such as a hits table, must be created per object in the constructor.

See it with the rest of the analogy cast

Official reference: create.roblox.com

Related terms