Metatables are often more helpful when it comes to object oriented programming or doing arithmetics on tables (like adding/subtracting…) because it allows multiple objects of the same class to exist while only referencing a single function which is useful when it comes to being conservative with memory, really it all comes back to memory.
Metatables are also useful for proxy tables, object wrappers (which I mentioned previously), locking properties among many others.
There’s a big difference between a library and an object. In this post, you’re creating a library to manipulate an object. Libraries and objects both are useful in their own ways. Libraries may contain objects but often they’re done for utility and utility only. Libraries are useful when it comes to having complex functions that are related in some way. For example, string and math are both libraries but they both do not contain any object creation. I guess, looking at it, libraries usually work with constant values. A library will have many values inside of them that never change. For example, math.floor is a function obviously, and this value will never change. Likewise, math.pi is a constant number that exists so you don’t have to type out 3.1415926… every time you need to do something that relates to a circle.
I guess the biggest takeaway is that libraries contain objects and values that never change. They’re also useful so you don’t have to rewrite the same function over and over which again is unwise for memory reasons.
The difference between a library and an object is that an object will have variable properties, and usually objects represent something. For example, a part is an object. Every part has its own individual properties, every part is standalone, this goes with any object.
The reason metatables are useful for OOP is for memory reasons (which I keep reiterating on, sorry lmao). Every class should have its own set of functions, however these functions are not per-object, they are a single constant table that exists between every object. 100 objects, 1 table of functions.
For example, if we attempt to compare two instances’ “destroy” functions, they will be the same, despite the two instances not being equal to each other. Think of the class functions to be like a library.
local p1 = Instance.new('Part')
local p2 = Instance.new('Part')
print(p1 == p2) -- false, they are two different objects
print(p1.Destroy == p2.Destroy) -- true, although these are two different objects, they share the same "Destroy" function
That’s up to you. If you want to set these properties, you can do something like:
function object.new()
local newObj = {}
newObj.WalkSpeed = 16
newObj.Health = 100
newObj.Changed = signal.new() -- I'd recommend using a custom signal object since bindable events are inefficient but it's up to you
return setmetatable(newObj, object)
end
However, it isn’t possible to detect any changes inside the table currently, you’d have to use a proxy table which is another concept that also relies on metatables but yeah.