Roblox Oop / Classes

I’m starting to get into OOP/classes; however, I have a couple of questions.

Now, bear with me as I’m very new to this, and to be quite honest, I could be doing this entirely wrong.

I have a few questions and would like to know if I have the general correct idea of what classes/OOP is.

  • Let’s say I had a folder in server storage called “Classes.”

  • As a child of “Classes,” I’d have a module titled “BaseCharacter,” with the idea in mind that characters are essentially different agents/heroes/operators (think Overwatch/Valorant-style characters).

  • “BaseCharacter” being the template, then I’d have (subclasses?) modules of the actual characters that alter their health, walk speed, character model, etc.

1: Is this a correct understanding of how classes work?
2: Would this mean on the client I’d need to essentially mirror these classes and have the visual logic. For example, VFX, animations, perhaps client-side hit detection in the mirrored version? If not, how would the client preform the correct actions?

3 Likes

You really not need OOP for this sort of things.
Instances are already OOP enough and dropping more abstraction is just madness.
Also there is no such thing as just plane “OOP” in Luau.
There is Metatable,Closure,Table (with method referances),Buffer OOP and it has to be specified about what you are talking about but i will assume you talk about metatable OOP.

You better just have attributes and functions that do certain action.
For internal stuff you may use a hash table.

Just dont listen to propaganda that “OOP is good” NO!
Its NOT GOOD
Its just a programming method and it has no benefits over any other
Everything has its usecases.

2 Likes

This sounds like a great idea. Implement these as ModuleScripts but if you don’t want to spend too much time on them (remember, don’t make it too complicated), try to add an instance inside of each that contains the character’s delta stats that can be copied to the remaining BaseCharacter.

4 Likes

What? OOP is good? It is literally one of the most popular paradigms learned by all programmers on all programming levels? But yes, I agree, everything has its use cases.

Instances take up more memory than one singular module, in addition, using values or attributes is way harder than making just a single class with all the values, as having a web of instances is honestly horrible, inefficient and unclean. Unlike a clean module class where everything is organised.

and having stacks of values like BoolValue is more abstraction than any class can get.

let me know if im not understanding something, as im not sure what you mean by “Instances”.

This is literally one of the perfect use cases for OOP, the main BaseCharacter class being the hero/agent with main/common logic, and then subclass specific behaviour using inheritance. And polymorphism makes handling different character abilities so much easier.

Thing you described is not even OOP, its a wrapper.
You don’t know what you are talking about.

You could be right that classes and this oop sort of stuff isnt needed here in my scenario, I dont know… but either way its good to have a bit of everything in your aresnal don’t you think?

Have you previously used OOP yourself, why didnt you like it?

like @SubtotalAnt8185 said, this sounds like a great idea.

in my opinion, i think you got it in the bag with this one man, this would technically be considered the general understanding of it :+1:

1 Like

This is correct, and it’s important to realize that you do NOT need OOP for this.

OOP would be useful if you were to say, create abilities which have abilities inside of them. You’re never going to do that, but if you do, it would be perfect for that.

OOP has own usecases and functional programming has own.
Don’t turn OOP into a plagua its not a universal programming technique that has to be used everywhere.

You can just make a function for that.
Handle important data in hash table and retrive it from there etc.
OOP would make it more annoying if you need to use data in other script becouse it will limit your control in this case and would make use of parallel Luau impossible in this case.

As for abilities switch table seems like the best option and it looks pretty easy to read etc:

local switch:{[string]:(Plr:Player)->()} = {
	Attack = function(Plr:Player):()

	end;
	_default = function(Plr:Player):() end
}
local function UseAbility(Plr:Player,Ability:string):()
	(switch[Ability] or switch._default)(Plr)
end

1: So i’m correct in saying when dealing with these sort of classes in the server I should make a mirrored version for the client? – (edit: Just read a newer reply, so yes I think this is correct)

2: What do you mean by I should add an instance inside of each module?

Respectfully, this is a literal form of OOP, it is NOT a wrapper.

Having a main class of BaseCharacter with all the common perks of all characters, and then subclasses of abilities using inheritance is a textbook example of OOP.

Then explain to me what is “true” OOP in luau? I might be missing something.

And calm down please, I wasn’t arguing with you.

The instance should be the character, or at least the changed parts of the character. Think effects, tools, ability scripts, etc.

Creating a mirrored version on the client is up to you, but if you want your client to be able to understand the stats of the character, it might be easier to just have these in ReplicatedStorage

So youre saying I shouldnt use classes here?

You don’t need to. You can create a ModuleScript for each character and set its stats there or just create a lot of modules. These aren’t “classes”, because this isn’t OOP.

1 Like

That a bloat and would make development boring+annoying+hard

Hashtable+switch+functional programming is a way to go.

Don’t turn OOP into a plagua :handshake:

So if youre saying classes arent a good use case here, where is?

You will create a data table, which isn’t necessarily a class.

But I was referring to classes…?

The whole meta tables, class.new() thats what I am currently trying to learn, which is a class?

I respect your opinion.
But calling OOP “bloat” doesn’t make it generally bad.
It is one of the most used paradigms across all fields of programming for a reason.
If you prefer hash tables, it doesn’t mean OOP doesn’t belong here.

Don’t call things “plagues” if you don’t like it. Im sure plenty other people do, even outside Roblox.