What is classes in OOP

Can someone explain to me what classes are used for in object orient programming thanks

1 Like

Classes are used as a “blueprint” of sorts for creating objects

A key principle in object oriented programming is “dont repeat yourself”, or D.R.Y.
Using a class allows you to use a blueprint to make the same thing over and over again rather than copy-pasting the code 5000 times

For example, you could make a “sword” class that contains all of the necessary code to make a basic sword, then you create sword objects from the blueprint class and you can make many swords and even different types of swords from one blueprint so you dont have to repeat yourself

A common comparison is:
Blueprint->House
Class->Object

1 Like

By sword objects do you mean methods using : operator?

I just mean as a general example of the use for classes

Like if you were making a game with swords in it

1 Like

So to understand classes they are used to hold the main functions including the design and methods are used to make the sword function like damage or animations?

Yes, they also can hold variables and stuff like that
In lua classes are basically tables that hold stuff that does stuff

If I make a sword or any weapon should I use OOP?

Its really entirely optional
I personally didnt use OOP until 4 years into scripting, however it wouldnt hurt to start early as long as you still actually understand what youre doing
I wouldnt sacrifice tons of time trying to figure out what in the world I was typing at 4 AM last night just so I can organize my script in a certain way
OOP has its benefits but if you dont feel like you would fully understand it or get into it and are just completely confused I would just do scripts normally

1 Like

This is my second to question. So if I am making a sword, the function Sword.new() should hold values like damage, player health, skins, basically creating the setup?

local Sword = {}

Sword.__index = Sword

function Sword.new(self)
	self.Health = 100
	self.Damage = 10
	self.Equipped = false
	
	return setmetatable({}, Sword)
end

function Sword:TakeDamage()
	self.Health = self.Health - self.Damage
	print(self.Health)
end


local NewSword = Sword.new(Sword)

Yeah that’d be pretty normal formatting for a roblox OOP setup

You probably wouldnt have the health inside the sword itself also but it still works

1 Like

This is my last question. Is this the best way to make a sword using OOP if not what can I change. (I know it doesn’t take actual damage to anything)

local Tool = script.Parent.Parent:WaitForChild("Tool")
local Mouse = game.Players.LocalPlayer:GetMouse()
local Sword = {}

Sword.__index = Sword

function Sword.new(self)
	self.Health = 100
	self.Damage = 10
	self.Equipped = false

	return setmetatable({}, Sword)
end

function Sword:TakeDamage()
	self.Health = self.Health - self.Damage
	print(self.Health)
end

function Sword:Equip()
	self.Equipped = true
	print("Tool equipped")
end

function Sword:Unequip()
	self.Equipped = false
	print("Tool unequipped")
end

local NewSword = Sword.new(Sword)

Tool.Equipped:Connect(function()
	Sword:Equip()
end)

Tool.Unequipped:Connect(function()
	Sword:Unequip()
end)

Tool.Activated:Connect(function()
	if Mouse.Target then
		Sword:TakeDamage()
	end
end)

I mean theres a million different ways of making a class, and its really a pretty creative process
A lot of the times you just have to go with your gut and go from there
OOP is made mostly to suit the user, so usually you just do what works best for you or the people youre working with

2 Likes