What is oop and why should I use it?

I wanna know what oop is and why everyone uses it.

What do you mean by “oop”? I have never heard of something called “oop” related to scripting but then I haven’t been scripting for that long.

It is object oriented programming I think it helps organize your game Idk never used it so thats why i have this question

@incapaz has already made a great explanation in the post below, I don’t think I can summarise oop simple enough, or if I did it would be not comprehensive enough.

Personally, I’ll try to personalize the, “Why use OOP question?” from my opinion.

You can reuse complex code fast.

Disclaimer: there are sometimes other better ways to reuse code other than OOP like entity component systems (ECS) which is what I’m using for now due to the issues with OOP like the diamond problem so yeah… just be mindfull of that

An example would be my turret controller;

Using this module you can make a turret look at a direction, or even your character look at a direction…like a turret :laughing:

So yeah it’s usefull 10/10 recommend.


I’m kind of shaky with the definition of oop but if I understand right it means storing variables and functions into “folders.” It can be used to organize data and stuff and create data structures. In lua, tables are those folders.

I’ll give an example of when it’s useful. Let’s say in your game you have 100 bugs, you’re going to want to store each bug’s health (which starts at 100). Without using tables you’d have to create a variable for each bug’s health.

local Bug1Health = 100
local Bug2Health = 100
local Bug3Health = 100
local Bug4Health = 100
local Bug5Health = 100
local Bug6Health = 100
local Bug7Health = 100
local Bug8Health = 100
local Bug9Health = 100
local Bug10Health = 100

But with oop, you can create one table to store a bug’s health and make 100 copies of that table (or “folder”)

-- This function creates a new Bug table
local function newBug()
    local Bug = {Health = 100}
    return Bug

-- All the bug tables will be stored in this array
local Bugs = {}

-- This creates 100 copies of the Bug table
for i = 1, 100 do
    local NewBugObject = newBug()
    table.insert(Bugs, NewBugObject)

print(Bugs) -- {{Health = 100}, {Health = 100}, {Health = 100}, etc}

Saving data for each bug’s health would have taken 100 lines without oop because I would have had to create a variable for each bug. With oop I just needed to create one “folder” (table) and copy it 100 times. Some tasks like this would be almost impossible without oop.

This is false, I use tables extensively while not following the paradigm.

I think the non-OOP example is very contrived to make OOP look better or whatever, you can simply do

local Bugs = { }

for i = 1, 100 do
    Bugs["Bug" .. i .. "Health"] = 100

without the need to make an entire bug class. That is just an example, but hey, it’s even shorter since there is no bug constructor.

Sorry, I removed that line.

That’s true, but what if you also wanted to store each bug’s color, type, damage, and a couple methods, that method would be very messy. I would prefer to have a table per bug so I can just reference one and have all the data right there instead of fishing around inside a massive table.

What is OOP?
OOP stands for Object Oriented Programming.
It is a programming concept where the code structure resembles that of a real life object.

For example: If I want to code a Seat, there would be attributes(or variables) such as “number of legs”, “number of seats and material”. All of these will be bundled up inside a single “Blueprint” aka Class.

If I create this “Seat” with 4 legs, 1 seat, and made out of wood.
It would become a Stool.

What is Classes?
Classes can be visualized as a Blueprint.
It details what an Object looks like (Properties) and what it can do (Behaviors).
All of these information makes up one object.

Classes needs to be instantiated to be use.
Meaning, if you buy a piece of furniture from IKEA, or Walmart, having the Blueprint by itself doesn’t do anything. You need to assemble it (Instantiate) so it becomes a physical object.

The advantage of using a Class over structural programming is that I can create a very complex object with only one variable. You have seen this already inside of Roblox Studio. Things like Instance, CFrame or Part.

They are all Classes working together to form a game.

Whenever you are using codes like:

Instance.new( "Part" );
CFrame.new( 0, 0, 0 );

You are instantiating or creating a new object.

Properties are easily understood. They are variables that represents the Object itself to provide additional information, such as world coordinates, color, name, etc.


Behaviors are the action the object can perform typically in the form of a function.

Part:Destroy( );

What is Encapsulation?
What’s powerful about OOP is the ability to Encapsulate, meaning to keep variables from being seen by public to prevent it from accidental or deliberate modification. It also improves security since the variables can only be modify through the Behaviors of that Class.

Unlike Structural Programming where you need a lot of variables to define an object and they’re often exposed in open space.

//This is C++ Code Example...
class Seat { 
  private:            // Access specifier
    int numOfLegs;    // Attribute (How many Legs it has)
    int numOfSeats;   // Attribute (How many Seat it has)

    // Behavior (Read Only, Return how many Legs)
    int getLegs( ) {  
      return this.numOfLegs;

If this object were to be created, there is no way of accessing the Legs or the Seats directly. The only way we can access that information is with a public behavior that is limited to reading the property only.

What is Inheritance?
Another feature of OOP that is extremely powerful when creating a large library of code is the ability to form relationships between Objects called “Inheritance”. This allows you to create new Classes from already existing Classes and all you need to do is provide the missing properties and behavior.

In the previous example with the “Seat Class”. By Instantiating the Seat with different attribute values, you can create all sorts of goofy Seats. However, you cannot create a chair from a Seat, because the Seat Class is missing a Back.

The Seat is a good start, because a Chair IS A seat, the only thing we need to add is a Back. In this case, we can create a Chair Class that Inherit from Seat Class.

//This is C++ Code Example...
class Seat { 
  private:            // Access specifier
    int numOfLegs;    // Attribute (How many Legs it has)
    int numOfSeats;   // Attribute (How many Seat it has)

    // Behavior (Read Only, Return how many Legs)
    int getLegs( ) {  
      return this.numOfLegs;

// This is Inheritance
class Chair: public Seat {
    int numOfBacks;

What this does is, the Chair Class will have all the properties and behavior of a Seat.
The only thing new about the Chair Class is the “numOfBacks” because all other information already exist from the Seat Class.

Where will you find this type of relationship inside of Roblox Studio?

Model:GetChildren( );
Part:Destroy( );

I believe a lot Classes in Roblox Studio are inherited from the Instance Class.
Whenever you use :GetChildren( ), or :Destroy( ). They are behaviors of the Instance class and not the Part or the Model.

The last concept of OOP is polymorphism, which basically “the ability to take many different forms”. This won’t make much sense until you actually start coding in OOP.

To give a quick example of what it looks like in real life.
Let’s say you have several kinds of Animals, “Dog”, “Kangaroo”, and “Fish”.
They are all different in shapes and size, but they share some commonalities like “Eating” and “Moving”, but the WAY they Eat and Move is implemented differently.

Dog = Run on 4 Legs
Kangaroo = Hops of 2 Legs
Fish = Swims with Fins

What Polymorphism allows them to do in a programming sense.
It allows a program to execute “Eating” and “Moving” behavior as an Animal.
So they’ll all do different things, but uses the same execution.

I think I have written enough about OOP for one day.
Time for me to go back to my own work. :stuck_out_tongue: