I’m trying to get better at object oriented programming. I have a module script that gives each players character a table of ‘stats’ which are things like health, armor, different ammo types, etc. Now what I want is an easy way of adjusting these stats based on the players picking up the items. What I’m currently doing is this:
Folder in the workspace with all ‘pickups’ inside. Each ‘pickup’ has its own attributes assigned to. ‘Available’ true or false if it can be picked up. ‘Amount’ is the amount of increase it gives to the stat. And the name of the pickup itself will be something like ‘Health’ or ‘Armor’ so we know which stat it is increasing. I could make this system work with all the stuff I need it to, but like I said I’m trying to get better at object oriented programming and I was hoping someone could lead me in a better direction because all the checks I have to make whenever a player steps on an item I feel is defeating the purpose of trying to go the OOP route. So I’ll show my code below so you can see what I mean and hopefully someone that’s done something like this before could just let me know a highly efficient/better way.
--Module Script
local Character = {}
Character.__index = Character
local charStats = {}--This table just keeps hold of all the characters stats and will allow them to be easily accessed
local Pickups = workspace.Pickups:GetChildren()
function Character.WipeStats(Char)--just sets a characters stats to nil to wipe them when need be
charStats[Char] = nil
end
function Character.CreateNewStats(Char,MAXHP,HP,ARM,MAXARM,MGA,SGA)--Each character will have stats (hp/armor, ammo, etc)
local newStats = setmetatable({},Character)
Char.Humanoid.MaxHealth = MAXHP
Char.Humanoid.Health = HP
Char.Humanoid:SetAttribute("Armor",ARM)
newStats.MaxArmor = MAXARM
newStats.MachinegunAmmo = MGA
newStats.ShotgunAmmo = SGA
charStats[Char] = newStats
Char.Humanoid.Died:Connect(function()
Character.WipeStats(Char)
end)
return newStats
end
function Character.Pickup(Char,Item)
Item:SetAttribute("Available",false)--Like I said I have 'available' attribute that sees if
end
--This function is what I'm really talking about. I will have to make big checks for every different item. Also not all items that can be picked up will be as simple as health/armor.
--Yes this system does already work but I want to understand how to make this better/more efficient/more robust and managable
function Character.CheckPickup(Humanoid,Item)
local Char = Humanoid.Parent
local CStats = charStats[Char]
local Maximum = Item:GetAttribute("Max")
local Amount = Item:GetAttribute("Amount")
local Armor = Humanoid:GetAttribute("Armor")
if Item.Name == "Health" and Humanoid.Health < Humanoid.MaxHealth and Humanoid.Health < Maximum then
Humanoid.Health = Humanoid.Health + Amount
Character.Pickup(Char,Item)
elseif Item.Name == "Armor" and Armor < CStats.MaxArmor and Armor < Maximum then
local newArmor = Armor + Amount
if newArmor > CStats.MaxArmor then newArmor = CStats.MaxArmor end
Humanoid:SetAttribute("Armor",newArmor)
Character.Pickup(Char,Item)
end
end
for i = 1, #Pickups do
local Item = Pickups[i]
Item.Touched:Connect(function(Touch)
local Humanoid = Touch.Parent:FindFirstChild("Humanoid")
if Humanoid and Item:GetAttribute("Available") == true then
Character.CheckPickup(Humanoid,Item)
end
end)
end
return Character