Questions about OOP

and I oop

I’ve read several threads on the DevForum mentioning OOP, but none quite explain what the usefulness of OOP is.

With reading all of these threads comes questions I must ask before I begin scripting one of my new projects:

  1. Why is OOP useful as opposed to what some consider “regular” scripting?
  2. Is there a significant performance benefit to using OOP?
  3. What are the best resources for a beginner to learn more about the fundamentals of OOP?

There will be a lot of different opinions on this. This is just my opinion, and I’m sure people might have other thoughts that are valid yet different:

1. Why is OOP useful?
Well, first of all, very few Roblox scripters stick to a single paradigm, but mostly because Lua pretty much lets you do whatever you want (it’s considered “multi-paradigm”). Following OOP practices will help you stick with a paradigm (e.g. a way/method of coding) which usually results in cleaner & more organized code. There are a lot of pros/cons to OOP in particular. I recommend just googling “OOP pros/cons” and reading through various opinions.

One thing to note is that Lua does not support OOP out of the box. In other words, Lua has no “class” structure built-in. Therefore, we have to use a pseudo-class technique through metatables. It works fine though, but just adds some additional code overhead (AKA “boilerplate” code).

2. Is there a significant performance benefit
This really depends on how you use OOP, and what you’re looking at.

Because Lua OOP requires using metatables, your code has to go through more operations to do a task. This little overhead is typically so small that it’s not worth measuring.

Regarding memory, you actually save memory when it comes to methods/functions attached to the object. If you have 10,000 NPCs in your game via some OOP objects you’ve made, they all technically share the same functions, thus you don’t have 10,000 copies of the same functions.

Arguably, OOP practices will force you into certain coding techniques that might help performance at a higher level in some instances.

3. Resources for learning fundamentals

IMO work through magnalite’s tutorial (first one listed above) and you will learn just about everything you will ever need to know about OOP using Lua.


Good question. Crazyman32’s response probably answers your question better than mine. However, this is a question I have somewhat wondered about before, and I’ve seen it summarized in different places, so I wanted to leave my thoughts here. Basically the usefulness of object oriented programming is that you don’t have to rewrite tons of code. Think about functions. Instead of writing and rewriting the code every time you need to use it, you can write it once, and execute it many times over. There isn’t necessarily a performance benefit when you do this, the same code ultimately gets executed, but it can save a massive amount of development time (for example if you made a mistake in the code before you copy-pasted it everywhere, you’ll have to fix it in every. single. place. it was pasted). The main idea behind OOP is reusable code. Classes are very useful but Crazyman32 mentioned that they are not supported in Lua. OOP coding methods are very useful in game design, where you want need to replicate the behavior of something multiple times. Think of Roblox’s BasePart. It has a lot of properties that you can modify, and every single BasePart has those modifiable properties. You can create a new BasePart, and it inherits all of those properties.

I realize this is a rather long block of text containing my thoughts on OOP but it can be a big subject :slight_smile:. If you do a lot of scripting you’re probably using OOP to some extent whether you fully know what it is or not.

To summarize: OOP is about reusable code, it saves development time through how the code is written, but it doesn’t necessarily improve performance.


OOP allows you to quickly reuse code and modify them. You can duplicate the results of OOP in procedural code, but it will be more inefficient and time consuming.

(Due to the lack of examples in this thread, let me supply you with one :smile:)
Let’s say you are making a shooter game with different types of guns that deal different damage, have different reload speed, et cetera. You can use procedural code to write code for each gun. When the player clicks the screen, a bullet fires from the gun:

local reloaded = true

function fireBullet()
    if reloaded then
        -- player shot takes x damage
        reloaded = false
        reloaded = true


You can duplicate this code inside all the guns, change its reloadSpeed and its damage. But come on, who likes copying and pasting for every gun they have in their game? (Especially when you have, like, hundreds of them)

With OOP you can reuse code and easily change the object’s (in this case, the gun) properties. You can change the fireBullet function just inside the ModuleScript and the change will duplicate across all guns. Or maybe you could add in new functions, properties without having to change all gun scripts.

One handy property of OOP is inheritance. Lets say you want to create a new type of gun that shoot heat-tracking rockets that follow players. You can create a new class that inherits from the main gun class and name it something like rocketGun, and then just overwrite the fireBullet function. You won’t have to bind the mouse click event again, since you only overwrote the fireBullet function, and not the initialize function.

Tbh, I don’t know… :sweat_smile:
But, quoting @Crazyman32:

Yes, I definitely agree :slight_smile:

Well, I recommend watching/reading tutorials on the web. There seems to be very little material on Lua OOP, so if you know another language, you can try to learn OOP in that language (I recommend Python).

After you understand the basics, going through some of the intermediate or advanced tutorials about Lua OOP would be more easy. Also, read some example OOP code and try to understand it. You should also learn about metatables before diving into Lua OOP (it helps).

@Crazyman32 also dug up some interesting material on OOP (nice! :smile:).

And lastly, practice practice practice, as it is the only way to master programming.

1 Like