How to use oop?

Heyo trying to make game that has different playable characters that have their own hats which is used as a weapon each character has their own unique abilities and stats for example one character might be slow and has a fast hat speed or one character might have multiple jumps so I think oop could help, but how? lol

my idea was to have a main module that has functions that all hats will have which is the ability to be thrown and touched and then for each character I’ll make another module which will have specific functions for abilities and stats for that character however my coding sucks so I haven’t been able to do this. Heres what I currently have.

local Hat =,"Birthday")
function Hat:Throw()

function Hat:Touched()

	local NewHat = {}
	local HatClone = Main.ServStorage.Hats[HatType]:Clone()
	HatClone.CFrame = Character.Head.CFrame
	HatClone.Parent = workspace

	local BP ="BodyPosition")
	BP.P = 9000
	BP.Parent = HatClone

	local HatOwner ="StringValue")
	HatOwner.Parent = HatClone
	HatOwner.Name = "HatOwner"
	HatOwner.Value = Character.Name

	--| Prevent hat from moving on Z axis
	local MiddlePoint = workspace.Stage.MiddlePoint
		HatClone.CFrame =,HatClone.CFrame.Y,MiddlePoint.CFrame.Z)		
		BP.Position = Character.Head.Position +,1.4,0)	

	--| Hat Properties
	Hat.Speed = 20
	Hat.HatType = HatType
	Hat.PickedUp = true
	Hat.IsFlying = false
	Hat.OnGround = true
	Hat.HasBubble = false
	Hat.Owner = Character.Name
	return NewHat
1 Like

There is example of OOP

local CarModule = {}
CarModule.__index = CarModule

function CarModule:Honk()
  print("Honk from " .. self.CarName)


local self = setmetatable({},CarModule)
self.Color = Color.fromRGB(255,0,0)
self.CarName = "CoolCar"
self.Speed = 50

return self 


Here is a great tutorial on how to use oop in roblox. I suggest you read it fully (and maybe some of the replies). I’ve used it to start reworking my prototype game into OOP and I’m very satisfied with the functionality and practices the tutorial provided. How you structure your programming is completely up to you and how you want to organize it. In this case, I’d think using an object for the hat, as you said, would be the way to go, defining functions and properties common around all hats within the hat class. Unique functionality could be implemented by using a module script to store all the unique functions/abilities, and calling it when creating a new object, say though one of its properties or methods, or simply by having a separate module script for each unique ability/function. It’s up to you. Here is the template I used from the tutorial:

As @Znimator and @Monkepath have pointed out, metatables can recreate OOP in Lua. If you want to go beyond though you can look at Roblox-TS. Basically it compiles Typescript (which has OOP and static typing) into Lua.

The code for the main module and separate modules are pretty much the same. The only difference is that instead of creating a self variable, you’ll create a new mainmodule instance and then use that as self.

Separate module code:

local mainModule = require() -- Path to the main module

local characterModule = {}
characterModule.__index = characterModule

    local newCharacter =
    setmetatable(newCharacter, characterModule)

    return newCharacter

-- Functions for Character Class

return characterModule

There is also a official tutorial by lua.

Check out this post, its the best one out there on OOP in Roblox

Ok I just wanna clear some confusion? because a lot of people have redirected me to posts to learn about OOP meanwhile I’ve already learned about OOP in the past lol. The post you linked was where I originally learned it and then I also did my own practicing coding

Person1 ="Dom","Male","18",80,120,50)
NPC = require(game.ReplicatedStorage.NPC)

Warrior = {}
Warrior.__index = Warrior
setmetatable(Warrior, NPC)

function Warrior:Strike()

function,Gender,Age,HP,MP,PhysicalDEF)physical atacks
	local NewWarrior =,Gender,Age,HP,MP) 
	setmetatable(NewWarrior, Warrior) 
	NewWarrior.PhysicalDEF = PhysicalDEF
	return NewWarrior

return Warrior
NPC = {}
NPC.__index = NPC

function NPC:Info()
	local Pronoun = ""
	if self.Gender == "Male" then
		Pronoun = "he"
	elseif self.Gender == "Female" then
		Pronoun = "she"
	print("This is "..self.Name.." "..Pronoun.." is "..self.Age)

function NPC:ChooseClass(Class)
	self.Class = Class

	local NewNPC = {}
	setmetatable(NewNPC, NPC)

	NewNPC.Name = Name
	NewNPC.Gender = Gender
	NewNPC.Age = Age

	return NewNPC

return NPC

My issue is I don’t know how to actually use it for more complex things for example I wanna try and make a part using OOP but how could I use .Touched with it or if I have 2 players and I setup something for them with OOP how could I like access each one I’m not sure how I’d do stuff like that with OOP, but other than that I think I understand the basics