Learning metatables

So, i learned the basics of metatables, i took a long time(1 day to be exact), and i finally understand how it works and how to use it with OOP(ik we should use ECS now, but i am learning from scratch)

I’m mostly learning to start making comissions +10k robux atleast

And i would like to know if should i go deep in each aspect i study, because i took 1 day to learn the basics and the theory behind it, but when i started looking at scripts of people who use it, uhhhh, how can i explain:

Like, WHAT THE * IS THAT?
Class.__index = function(self, Index)
return self.BackingTable[Index]
end
I SEARCHED about this and found out that this is to controll how index works, and i ask if should i go that deep to get those +10k robux comissions, are they that hard?

I also ask myself how can those people from many years ago be so smart but so unorganized…

i’ve got these “whys” to the code:

  • why Service.Tool mimics Instance.new pattern? why it’s not PropaneTank.new(args)?
    Instance.new dives into native code and thus faster, while Service.Tool.new is most likely script with tons of ifs inside.
    also new with args would return initialized instance already without need to initialize it externally (what happens in this code).

  • seems __index and __newindex hit always, the latter one is actually a tzar handler that handles everything. Whenever user does PropaneTank.property = N the code runs through all these ifs.
    Both do not look great from performance point of view.

This is rhetorical, I actually don’t care about explanations about this weird design.

You dont need to understand metatables for OOP, but as far as I know metatables have specific methods like .add() and .__index() basically when a new value is added to a metatable or something happens to it these methods will fire.

.__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.”

https://create.roblox.com/docs/luau/metatables

DON’T
Metatables are never to be used in Luau since they make useless indirections and actually hurt performance.
It’s not a “compile-time thingy”, it’s an actual process that hurts runtime.

I suggest you learn about buffers and ECS instead; those ones are actually useful and will help you understand better design patterns.

Note to not “use libraries for ECS” because it is a scam and people are trying to convince you to use a wrapper that bloats the game. ECS in Luau is just a simple multiple tables holding references through entity ID essentially.

TL;DR: Metatables are bad and not worth learning.
The same goes for OOP; the compiler doesn’t recognize it as a first-class implementation like in C++ and it will be hurting runtime.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.