OOP for status effects

I am currently learning OOP and trying to make a Status Effects system using it. I am trying to make status effects stack so that if a player already has one it will increase it’s time and stack but when I try and do this the time does get increased but it prints “Status over” 2 times in the output? I am not sure how to get it so if a Status Effect exists on a player it will just increase the duration. This code runs on the Server and not Client. Here is my current code:

--// VARIABLES
local StatusEffectsList = {
	["BattleFocus"] = {
		Attributes = {
			["AbilityCooldownReduction"] = 20,
			["CriticalDamage"] = 10,
			["StaminaConsumption"] = 21
		},
		["Duration"] = 6,
		["Stackable"] = true,
		["Processing"] = false --For debounce
	}
}

local StatusEffects = {}
StatusEffects.__index = StatusEffects
--\\

function StatusEffects.new(StatusEffect, Character)
	local NewStatusEffect = {}
	NewStatusEffect[Character.Name] = {}
	setmetatable(NewStatusEffect, StatusEffects)
	
	NewStatusEffect[Character.Name][StatusEffect] = StatusEffectsList[StatusEffect]
	NewStatusEffect[Character.Name][StatusEffect].Time = workspace:GetServerTimeNow()
	NewStatusEffect.Connection = game:GetService("RunService").Heartbeat:Connect(function()
		if (workspace:GetServerTimeNow() - NewStatusEffect[Character.Name][StatusEffect].Time >= NewStatusEffect[Character.Name][StatusEffect].Duration) then
			NewStatusEffect.Connection:Disconnect()
			print("Status effect over")
		end
	end)
	
	return NewStatusEffect
end

function StatusEffects:RemoveStatusEffect(Character)
	print(self)
	setmetatable(self, nil)
	table.clear(self)
	table.freeze(self)
end

return StatusEffects
3 Likes

Duration isn’t defined anywhere

1 Like

Duration is defined at the top of the script. It waits the correct amount of duration it’s just if you try stacking status effects it prints “Status effect over” however many times you stacked the status effect

1 Like

Are you creating a new status effect object each time? As each status effect object would have it’s own connection.

1 Like

Yes I believe this is where I am going wrong as I am trying to get it to stack. Should I try storing new objects made with .new() into a table ?

1 Like

Yes. I tend to use the player instance to index a bit like this:

--server script 
local statEff = require(pathToModule)
local Alleffects = {} -- this could be another module script if you need to access the effects from other scripts.

Alleffects[player] = statEff.new()

I would also add a method to apply the effect which checks to see if it’s active then adds to the duration or reconnects it if not.
You can then just call the method from the table.

Alleffects[player]:AddEffect()
3 Likes

Thank you, this helped me solve my problem!

2 Likes

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