Ill keep this short, I’m trying to understand how to create classes and use OOP to make everything easier, but it ended up making everything more difficult
I read a bunch of articles about creating classes & metatables and all that, and I could probably do what I want to do if I just copied the code but I want to know WHY these people are doing what they’re doing
Im asking for someone to teach me the basics of creating a class in lua and how to use oop and module scripts and metatables so that I can have more organized code. Linking any useful articles that helped you would also be helpful
let me tell you
if you’re not planning to be a long-term roblox developer, i really wouldn’t suggest getting into it
feel free to ignore my advice, but metatables make me uncomfortable
basically if you wanna become a big name by making simulator or rng slop in roblox development, you should definitely or most likely learn OOP, but if you’re not planning to stay for long, i’d suggest not getting into it
yeah i know, you expected OOP advice, yet i gave you the exact opposite
I suggest watching B Ricey’s playlist on OOP. It is a bit lengthy but it helps you slowly understand.
You can then watch crusherfire’s tutorial on type notations which also help a bit.
OOP is used for anything that can be used more than once and also has different settings to them.
For example you can have a car module which has different speeds, colors, weights, and more that you could change any time.
It’s also helpful for things like Icon, which is a module a lot of developers use. It basically helps you make those top bar icon buttons and it’s structured something like this
I want to know why they’re writing the certain lines that they’re writing and what the purpose of it is/what it does. I’m aware that it all comes down to organization
I think this post does a pretty good job at explaining OOP in terms of LuaU. Although, It’s definitely a bit harder to understand if you haven’t used a language like Java or Python before (both have classes).
Regarding this, OOP is used everywhere in Roblox. Instances are literally OOP. Now, there are times where OOP shouldn’t be used, but if you use it at the correct moments, it’s definitely not a hindrance.
Personally I use OOP whenever there is something I want to be easily reusable. For example, a button class. Yes, Roblox already has ImageButtons/TextButtons, but I want to be able to extend that functionality further by adding tweens, sounds, etc.
I might start by planning out the “perfect API”
-- Example usage
AnimatedButton.new(existingButton, onClick)
The code below would take an existing Image/Text button and wrap the AnimatedButton class around it, also taking an onClick function that’s called whenever the button is activated.
Internally the class would be handling animations and sounds without me needing to know how all that works when I use it.
Again, the post I gave should go further into the principles of OOP and why you should use it, but this is just a small example of when you could use it and why.
Lua isn’t great for learning OOP. The reason why metatables are used in Lua OOP is because Lua isn’t actually an OOP language, it’s a “multi-paradigm” language which has the capabilities to imitate OOP. Metatables aren’t OOP, they’re just a tool we use to create the illusion of OOP in a non-OOP language.
If you really want to grasp the concept of OOP and inheritance/polymorphism, you should study a language like Java. Java syntax is simple to grasp and it’s explicitly designed for OOP unlike Lua. With Java, you get to focus more on OOP itself rather than fiddling around with metatables.
OOP at its core is a straightforward concept, but if your only exposure to OOP is in Lua, then you’re not really getting the full picture of what OOP is. If you actually want to study OOP, you should do so outside of Lua. It doesn’t matter if you’re never going to use Java, you should still learn it because it will actually teach you the intricacies of OOP in a way that Lua never could.
Roblox objects, like a part, is OOP. It gives you a convenient way to reason about data and actions you can take on it. You’re using OOP whether you like it or not!