What you’ll actually find is that a metatable has the exact same creation cost as a normal table because that’s all that it is.
I don’t see much use for metatables in admin commands, because commands are explicit to existing, there would be no need for the linkage between tables.
I’ll give you some more examples to broaden your horizons some. I would argue that the most common use of metatables across all of Luau is custom libraries / custom objects / OOP. If you’re going to learn to write reasonable inheritance, then you can and should be using metatables. Here’s an example.
local Class = {}
Class.__index = Class
function Class.new(t)
setmetatable(t, Class)
return t
end
This is an incredibly basic constructor used to build a custom class. We can write Variable = Class.new{}
and instantly have a table that exists under this new bracket. Calling an empty function inside of this new variable will immediately search inside of Class.
Here’s why it matters:
function Class:Clear()
return Class.new{}
end
local Example = Class.new{}
Without an attached metatable with an __index metamethod, we cannot write Example:Clear() unless that function is wrote explicitly inside of the table provided by our variable. Writing to a metatable across numerous objects saves you memory by allowing you to direct functions from inside of your class specifically. It also allows you to write default values for your class. Let’s look at that next. We’ll start by writing an empty property inside of our class.
Class.Defaults = {x = 100, y = 100, z = 100}
function Class.new(t, ...)
local new = t
new.Values = {}
setmetatable(new, Class)
setmetatable(new.Values, {__index = Class.Defaults})
return new
end
Next, we’ll write a function to increment our object even though we never explicitly gave it any values (however by adding … to our function parameters we could catch values in the future. This is how classes work in nature. Whenever you write Vector3.new() without passing arguments you are indexing the vector metatable’s value defaults. You can prove this to yourself by writing print(getmetatable(Vector3.new()))
, which will provide that the metatable is locked because it holds the __metatable metamethod.
My point in saying that is because Roblox up until and including where it is wrote in C, is wrote into classes. That is how and why inheritance works. Roblox is a hierarchy.
Looking back at our example, we can now write a function to increment our class about an axis.
function Class:Increment(byAxis)
self.Values[byAxis] += 1
end
Example:Increment("x")
print(Example.Values.x) -- 101
Full example for further practice (click to open)
local Class = {}
Class.Defaults = {x = 100, y = 100, z = 100}
Class.__index = Class
function Class.new(t, ...)
local new = t
new.Values = {}
setmetatable(new, Class)
setmetatable(new.Values, {__index = Class.Defaults})
return new
end
function Class:Clear()
return Class.new{}
end
function Class:Increment(byAxis)
self.Values[byAxis] += 1
end
local Example = Class.new{}
Example:Increment("x")
print(Example.Values.x) -- 101
Example = Example:Clear()
print(Example.Values.x) -- 100
Metatables are worth fully understanding and doing research on, because using them results in incredibly powerful structures with practice.
There are some notable differences between vanilla Lua metatables and Luau metatables.
- __gc (fires when table is garbage collected) does not work with Luau.
- References to Roblox instances are never weak – this is to say that there is differences in __mode performance.
Some other reasonable uses for metatables in Lua are: table math for greater readability, writing immutable tables without using new table.freeze()
method, building a “Changed
” event for tables by using a proxy table (similar to read-only).
Hopefully this helps you learn some, if you have any questions feel free to ask me. Metatables are absolutely worth using and learning. Anyone who tells you otherwise is speaking from a lack of experience.
You are incorrect. OOP is incredibly common within well made games on Roblox, why would it not be with the amount of benefits that it holds.
Any kind of source for this at all? Lua.org fully supports OOP regardless of the language’s nature, in fact the Lua guidebook has an entire chapter on it. Don’t make claims like this without a source.