CCCPaste

Debuff Module

-- DEBUFF HANDLER MODULE SCRIPT

local Debuff = {}
local RunService = game:GetService("RunService")
Debuff.__index = Debuff

Debuff.DebuffSchema = {
	Wither = {
		TickInterval = 1,
		Duration = 5,
		UniqueStatusData = {
			Name = "Withering",
			Damage = 5, 
			Slow = 6,
			Color = Color3.fromRGB(100,0,0)
		},
		
		OnApplied = function(EntityId,UniqueStatusData)
			print("Applied the debuff ",UniqueStatusData.Name," on ",EntityId.Name)
		end,
		
		OnRemoved = function(EntityId,UniqueStatusData)
			print("Removed the debuff ",UniqueStatusData.Name," on ",EntityId.Name)
		end,
		
		OnTick = function(EntityId,UniqueStatusData)
			print("Triggered the debuff ",UniqueStatusData.Name," on ",EntityId.Name)
			EntityId:TakeDamage(2)
		end,
		
	}
}

local ActiveDebuffs = {}

function Debuff.new(entityid,endtime,nexttick,schema)
	local now = os.clock()
	
	local self = setmetatable({}, Debuff)
	self.EntityId = entityid
	self.EndTime = now + endtime
	self.NextTick = now + nexttick
	
	-- Schema Cache is better to avoid table lookups every frame on schema
	self.TickInterval = schema.TickInterval
	self.Duration = schema.Duration
	self.UniqueStatusData = schema.UniqueStatusData
	
	self.OnApplied = schema.OnApplied
	self.OnRemoved = schema.OnRemoved
	self.OnTick = schema.OnTick
	
	self.OnApplied(self.EntityId,self.UniqueStatusData)
	
	table.insert(ActiveDebuffs,self)
	return self
end

function Debuff:_Update(now)
	if now > self.EndTime + 1 then -- I like to end it on 0
		self:Destroy()
		return false
	elseif now >= self.NextTick then
		self.NextTick = now + self.TickInterval
		self.OnTick(self.EntityId,self.UniqueStatusData)
	end
	return true
end

function Debuff:Destroy()
	self.OnRemoved(self.EntityId,self.UniqueStatusData)
end

RunService.Heartbeat:Connect(function()
	local now = os.clock()
	for i = #ActiveDebuffs, 1, -1 do
		local Debuff = ActiveDebuffs[i]
		if not Debuff:_Update(now) then
			ActiveDebuffs[i] = ActiveDebuffs[#ActiveDebuffs]
			ActiveDebuffs[#ActiveDebuffs] = nil
		end
	end
end)

return Debuff

-- DEBUFF HANDLER SERVER SCRIPT

local DebuffModule = require(game.ServerStorage.DebuffModule)
local EntityModule = require(game.ServerStorage.EntityMaker9000)
local RunService = game:GetService("RunService")

for _,Villager in game.Workspace.Entities:GetChildren() do
	local VillagerInQuestion = EntityModule.new(Villager,math.random(10,12),1,2)
	local LiterallyThePlague = DebuffModule.new(VillagerInQuestion,5,1,DebuffModule.DebuffSchema.Wither)
end