What actually is OOP?

I know how to use OOP, I know the 4 main pillars of OOP, I just don’t know its uses and purpose.

I have no idea of how or where I should use this concept in real scripts.

OOP can be summed up in making a module so as not to duplicate the script for each object or copy and paste each function, it is a more compact and orderly way of doing things, imagine the chaos and poor performance that would be having a script for each object.

Assuming you have the basic concept of what OOP is, here’s generally the usage and purpose:

Imagine that you have something in code that you have a lot of. For example, let’s say that you have a lobby in a large game server, where that server has many lobbies. All of these lobbies are going to be the same and use the same functions, except for very few things about them, such as the players, the time the lobby has existed, and settings of the game lobby. This is a great case where OOP can be put to use to provide a convenient way to organize the code for the lobby. Instead of having a table, and then having to independently find and call functions to move data around in a table, we can simply make methods that work on the table. OOP-style methods are actually very, very common in the ROBLOX API, the most common of which being Instances. This is where OOP shines.

To answer the how to use question, the (usual) implementation is to use metamethods with the .__index property on tables in order to have a table of functions, and then a table defining the object. You then have a constructor, .new() for example, that makes a new table, assign its properties, and then sets this metamethod. Conveniently, you can make the .__index metamethod simply reference a table. This is all in a ModuleScript.

Here’s (a recreation) of the example code that a friend gave to me years ago when I first learned OOP and how to apply it in ROBLOX. I hope it can provide a decent example:

local Loaf = {}

local BreadColors = {
    ["White"] = {
        ["Untoasted"] = "pale tan",
        ["Toasted"] = "brown",
    },
    ["Wheat"] = {
        ["Untoasted"] = "light brown",
        ["Toasted"] = "dark brown",
    },
}

function Loaf.new(Type, Slices)
    local NewBread = {}
    setmetatable(NewBread, {
        ["__index"] = Loaf
    }) -- With this, anything that tries to reference our bread that doesn't exist will then look for it in Loaf

    NewBread.Type = Type
    NewBread.Slices = Slices
    NewBread.Color = BreadColors[Type]["Untoasted"]
    NewBread.ToastedValue = 0

    return NewBread
end

function Loaf:Eat()
    if self.Slices <= 0 then
        print("There is no more bread in this loaf for you to eat!")
    else
        if self.ToastedValue == 0 then
            print("You have consumed a delicious piece of " .. self.Type .. " bread!")
        elseif self.ToastedValue == 1 then
            print("You have eaten a yummy piece of " .. self.Type .. " toast!")
        else
            print("Yuck! You've eaten a piece of burnt " .. self.Type .. " toast.")
        end
    end
    self.Slices -= 1
end

function Loaf:Toast()
    local CurrentColor = self.Color
    self.ToastedValue += 1
    
    if self.Slices > 0 then
        if self.ToastedValue == 1 then
            self.Color = BreadColors[self.Type]["Toasted"]
            print("You have toasted your loaf of bread from it's " .. CurrentColor .. " color to a shade of " .. self.Color .. ".")
        elseif self.ToastedValue == 2 then
            self.Color = "black"
            print("Uh oh! You have burnt your toast, making it a shade of black!")
        elseif self.ToastedValue > 2 then
            self.Color = "darker " .. self.Color
            print("You have burnt your burnt toast even more than it was burnt before, somehow, and now it is colored " .. self.Color .. ".")
        end
    else
        print("You can't toast bread that doesn't exist...")
    end
end

return Loaf

Module scripts aren’t really OOP. OOP would be defined as having multiple objects (which can really be described as more complex data types) that contain both values and methods. Values being things that it stores, and methods being functions you can use on said object (i.e. human.run()). OOP also allows inheritance, which would allow you to take the values and methods from one class and use it as a template in another class (for example, having an Animal class, and then having a Lion class that inherits the traits of the Animal class).