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 = Modules.HatLogic.new(Character,"Birthday")
function Hat:Throw()
warn("Trhow")
end
function Hat:Touched()
warn("Touched")
end
function Hat.new(Character,HatType)
local NewHat = {}
setmetatable(NewHat,Hat)
local HatClone = Main.ServStorage.Hats[HatType]:Clone()
HatClone.CFrame = Character.Head.CFrame
HatClone.Parent = workspace
local BP = Instance.new("BodyPosition")
BP.P = 9000
BP.Parent = HatClone
local HatOwner = Instance.new("StringValue")
HatOwner.Parent = HatClone
HatOwner.Name = "HatOwner"
HatOwner.Value = Character.Name
--| Prevent hat from moving on Z axis
local MiddlePoint = workspace.Stage.MiddlePoint
Main.RunService.Heartbeat:Connect(function()
HatClone.CFrame = CFrame.new(HatClone.CFrame.X,HatClone.CFrame.Y,MiddlePoint.CFrame.Z)
BP.Position = Character.Head.Position + Vector3.new(0,1.4,0)
end)
--| 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
end
local CarModule = {}
CarModule.__index = CarModule
function CarModule:Honk()
print("Honk from " .. self.CarName)
end
function CarModule.new()
local self = setmetatable({},CarModule)
self.Color = Color.fromRGB(255,0,0)
self.CarName = "CoolCar"
self.Speed = 50
return self
end
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
function characterModule.new()
local newCharacter = mainModule.new()
setmetatable(newCharacter, characterModule)
return newCharacter
end)
-- Functions for Character Class
return characterModule
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
NPC = require(game.ReplicatedStorage.NPC)
Warrior = {}
Warrior.__index = Warrior
setmetatable(Warrior, NPC)
function Warrior:Strike()
print("STRIKE")
end
function Warrior.new(Name,Gender,Age,HP,MP,PhysicalDEF)physical atacks
local NewWarrior = NPC.new(Name,Gender,Age,HP,MP)
setmetatable(NewWarrior, Warrior)
NewWarrior.PhysicalDEF = PhysicalDEF
return NewWarrior
end
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"
end
print("This is "..self.Name.." "..Pronoun.." is "..self.Age)
end
function NPC:ChooseClass(Class)
print(self.Class)
self.Class = Class
print(self.Class)
end
function NPC.new(Name,Gender,Age,HP,MP)
local NewNPC = {}
setmetatable(NewNPC, NPC)
NewNPC.Name = Name
NewNPC.Gender = Gender
NewNPC.Age = Age
NewNPC.HP = HP
NewNPC.MP = MP
return NewNPC
end
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