Use metatables to create reusable objects with constructors, methods, and inheritance — the pattern behind every Roblox API.
Every Roblox API you've used — Instance.new, :Play(), :WaitForChild() — works because something is an object with methods. Luau doesn't have a class keyword, but it has — the mechanism that makes object-oriented patterns work. If you've ever wondered how humanoid:LoadAnimation() works behind the scenes, this is it.
A is a that changes how another behaves. The most important operation is __index — it tells Lua what to do when you look up a key that doesn't exist:
localDog = {}
setmetatable(Dog, { __index = Dog })
When you ask for Dog.Bark and it doesn't exist, Lua checks __index. If __index is a , it calls it. If it's a , it looks up the key there. This is how methods get shared across many objects.
The metatable key that controls what happens when a missing key is looked up is .
A "class" in Luau is a with a constructor :
local Coin = {}
Coin.__index = Coin
function Coin.new(value)
local self = setmetatable({}, Coin)
self.value = value or 1
self.collected = false
return self
end
function Coin:Collect()
self.collected = true
return self.value
end
Coin.new(value) is the constructor — it creates a fresh , gives it the Coin , and returns it.Coin:Collect() is a method — the colon syntax passes self automatically.setmetatable({}, Coin) is the key line — it wires the new to look up missing keys in Coin.local gold = Coin.new(10)
print(gold.value) --> 10
local reward = gold:Collect()
print(reward) --> 10
gold is a plain with two values. But gold:Collect() works because when Collect isn't found on gold, Lua checks the 's __index (which is Coin) and finds it there.
Why does Coin.__index = Coin matter?
One class can inherit from another:
local Gem = setmetatable({}, { __index = Coin })
Gem.__index = Gem
function Gem.new(value, color)
local self = setmetatable(Coin.new(value), Gem)
self.color = color or "blue"
return self
end
function Gem:Glow()
return self.color .. " gem is glowing!"
end
Gem inherits from Coin — a Gem has .value, .collected, :Collect(), and its own :Glow(). If Gem doesn't define Collect, it falls through to Coin via the __index chain.
This is exactly how Roblox's class hierarchy works — Part inherits from BasePart which inherits from PVInstance which inherits from Instance.
Don't over-inherit. Two or three levels deep is usually the sweet spot. Deep inheritance chains are hard to debug.
OOP shines when you need many similar objects with shared behavior:
OOP is overkill for:
return is enough).A Enemy class has a constructor Enemy.new(name, health). Add a method TakeDamage that subtracts damage from health and returns the new health.
local Enemy = {}\nEnemy.__index = Enemy\n\nfunction Enemy.new(name, health)\n local self = setmetatable({}, Enemy)\n self.name = name\n self.health = health\n return self\nend\n\n-- add the TakeDamage methodsetmetatable. A constructor that returns a plain has no methods. setmetatable({}, ClassName) is the whole trick.function Coin.Collect(self) works but is error-prone. function Coin:Collect() passes self automatically..data instead of inheriting from a parent class.Coin.new() returns an . Coin:Collect() is a method on the class — calling it on an is wrong.Next: — splitting into sides.