I know Object Orientated Programming but I don't know when to use it?

I know how to use OOP. Example I just write:

local Module = {
	ClassName = "Module"
}
Module.__index = Module

function Module.Model(Model, Name, Parent)
	local NewModel = {}
	setmetatable(NewModel, Module)
	NewModel.Model = Model
	NewModel.Name = Name
	NewModel.Parent = Parent
	return NewModel
end

function Module:SetToManHood()
	local SelfClone = self.Model:Clone()
	SelfClone.Name = self.Name
	SelfClone.Parent = self.Parent
end

local BigTekazChild = Module.Model(game.ReplicatedStorage.BigTekaz, "SmallTekaz", workspace.Terrain)
BigTekazChild:SetToManHood()

But like when would it ever be useful at first I was like oh I can add custom properties to parts but you can’t. So then I was like when would this ever be useful?

I have looked everywhere but I just still don’t know why this would be useful.

If someone could give me an example that is not that stupid:

local Car = {
	ClassName = "Car"
}
Car.__index = Car

function Car.New(Driver, Nothing, Stupid)
	local NewCar = {
		Description = "Horrific Accient on the M5 eastbound to Norway. Hoverboards cause sugar levels to plumit. Fake nothingness forever."
	}
	setmetatable(NewCar, Car)
	NewCar.Driver = Driver
	NewCar.Nothing = Nothing
	NewCar.Stupid = Stupid
	return NewCar
end

function Car:RelapseWhatYouAre()
	for I, Am in pairs(self) do
		print(I .. ":", string.rep(" ", string.len(I)), Am)
	end
end

local Useless = Car.New("#BaconsAreForeverAlone!", "What this does!", "How stupid is this very very.")
Useless:RelapseWhatYouAre()

Car example is so annoying give me a good useful practical example please

anyway that is all.

In all sincerity, object-oriented programming is merely a preference and organizational tactic in Lua since the language itself doesn’t revolve around classes and inheritances. Any concept developed with class emulation in Lua can probably be recreated just as accurately, if not more efficiently, through caching and use of standalone functions. Emulated OOP does, however, have many advantages that mostly relate to readability and convenience as opposed to efficiency. We most often observe OOP being used throughout ROBLOX within public modules, such as @EtiTheSpirit’s FastCast for ease of implementation in personal code.

3 Likes

OOP doesn’t really exist in a natural state in Lua and it’s sort of “pseudo OOP” in the common sense. I personally avoid it as much as possible (personal preference) as I come from a different background.

If I had to use it in Roblox for practical reasons, it would be to make a base class for a “weapon” that had some general methods such as

weapon:EquipToCharacter()
weapon:GetValue()
weapon:Unequip()

Or for NPCs as a base class

npc:PathfindAndWalkToPoint()

But even then, that’s opinionated (as many people would read this and have agreement or disagreement on such). I wouldn’t want to use OOP for anything in Lua and it typically just gets a bit messy to keep up with.

For example, the “OOP” code you’ve shown just looks over complex for anything it would need to do and could be more native to the Roblox API if it wasn’t in Lua’s strange object-orientation.

My background is in C#, JS, and PHP - so my opinions are skewed by this.

2 Likes

Agreed.

I tried to give OOP programming a go one time, and it just seemed… messy? I remember so many kept raving on how OOP programming kept your eggs in line, but quite honestly it looks like is just makes things more messier.

1 Like

It’s very difficult to explain the use of Object Oriented Programming without going out and experiencing it. @anime_bb is correct, and the best way to experience this is to just design an entire project with complete object orientation

1 Like

How do I make my own function like workspace:GetName() or workspace:GetProperties() for any instance?

It isn’t possible; to define your own methods for workspace would be to alter the class itself. Defining additional methods for a predefined data object would be, to put it bluntly, just as pointless as it were impossible. In Lua, methods are simply functions with a predefined variable self that references the table in which the method was originally called from. Such behavior is most practical for OOP emulation, where the method could be called from any table associated with the metatable that contains the method. Having access to the original table without having to pass it an as an argument is only a convenience. If a method is indexed as a function (with a period . and not a colon :), the first argument passed in would represent self. Here is some code to clarify what I mean:

local tbl = {str = 'a string'};

function tbl:Method(str2)
	print(self.str, str2);
end

tbl:Method'and another string'; --outputs 'a string and another string'
tbl.Method(tbl, 'and another string'); --also outputs 'a string and another string', so they are essentially the same