Is this good OOP code?

So, I have been learning OOP, and this was my first time trying to actually make something work. This script changes the Color of a part, but I made it with OOP, let me know what I can improve on.

local Color = {}
Color.__index = Color

function Color.NewPart(part : BasePart)
	local self = setmetatable({}, Color)
	self.part = part
	
	return self
end

function Color:changeColor(color : Color3)
	self.part.Color = color
end

local Part = Color.NewPart(workspace.SpawnLocation)
Part:changeColor(Color3.new(1, 0, 0.159243))
1 Like

You’ve done pretty well, your code should work perfectly fine. If you want, you can use this tutorial to make yourself better at OOP:

Or you can use my easy-to-use module to create classes and objects without knowing complicated things about metatables: Class++ | Classes and OOP made easy and powerful with Access Specifiers, function overloading and more!

1 Like

I understand the concept of it now, I will check out your OOP Module, and read the tutorial, thank you!

1 Like

Yes this is proper OOP, usually by convention we use .new and typically methods are capitalized like :ChangeColor, just things I’ve observed.

(assuming you return Color at the end of your module)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.