You’ve run into a classic “where’s target coming from?” problem—your tick method doesn’t know which entity it’s ticking. You have two simple ways to fix this:
Store the target on the effect when you apply it
Pass the target into tick (and expire) every time
1) Store the target in the effect
Here you make apply remember who it was applied to, so tick can pull it back out:
lua
local StatusEffect = {}
StatusEffect.__index = StatusEffect
function StatusEffect.new(name, duration, onApply, onTick, onExpire, stackable)
local self = setmetatable({}, StatusEffect)
self.Name = name
self.Duration = duration or 0
self.Elapsed = 0
self.onApply = onApply or function() end
self.onTick = onTick or function() end
self.onExpire = onExpire or function() end
self.Stackable = stackable or false
self.Target = nil -- ← we’ll fill this in on apply
return self
end
function StatusEffect:apply(target)
self.Target = target
self.Elapsed = 0
self.onApply(target)
end
function StatusEffect:tick(deltaTime)
if not self.Target then return end
self.Elapsed = self.Elapsed + deltaTime
self.onTick(self.Target, deltaTime)
if self.Elapsed >= self.Duration then
self:expire()
end
end
function StatusEffect:expire()
if not self.Target then return end
self.onExpire(self.Target, true)
self.Target = nil
end
return StatusEffect
Now your ticker doesn’t need to pass target around. You just do:
lua
effect:apply(player) -- stores the target
... every frame ...
effect:tick(dt) -- the effect knows its target
2) Pass the target into tick (and expire)
If you’d rather keep your effect “stateless” and let the caller always say which target, you can change the signatures:
lua
function StatusEffect:apply(target)
self.Elapsed = 0
self.onApply(target)
end
function StatusEffect:tick(target, deltaTime)
self.Elapsed = self.Elapsed + deltaTime
self.onTick(target, deltaTime)
if self.Elapsed >= self.Duration then
self:expire(target)
end
end
function StatusEffect:expire(target, expiredByTime)
self.onExpire(target, expiredByTime)
end
Then your EffectTicker (or your loop) must do:
lua
effect:apply(target)
...
effect:tick(target, dt)
-- EffectTicker.update would become:
-- for target,effects in ...
-- effect:tick(target, dt)
Which to choose?
Storing the target is very handy if one effect only ever lives on one target at a time.
Passing the target makes your effect more functional/pure, but your ticker must always remember and pass in who’s being ticked.
Both approaches are equally common. Pick whichever fits your architecture best!
i need it to tick every second, actually make tickdelay something I can put in the core status effect
Assistant
Here’s one way to bake a per‐effect tick delay into your StatusEffect class so that “onTick” only fires once every N seconds—even if you drive your ticker every frame.
–––
1) Update StatusEffect to accept a tickDelay
lua
local StatusEffect = {}
StatusEffect.__index = StatusEffect
-- new(name, duration, tickDelay, onApply, onTick, onExpire, stackable)
function StatusEffect.new(name, duration, tickDelay, onApply, onTick, onExpire, stackable)
local self = setmetatable({}, StatusEffect)
self.Name = name
self.Duration = duration or 0 -- total lifetime
self.TickDelay = tickDelay or 1 -- seconds between onTick calls
self.onApply = onApply or function() end
self.onTick = onTick or function() end
self.onExpire = onExpire or function() end
self.Stackable = stackable or false
-- internal timers
self._elapsed = 0 -- total time since apply
self._tickAccumulator= 0 -- time since last onTick
self._target = nil
return self
end
function StatusEffect:apply(target)
self._target = target
self._elapsed = 0
self._tickAccumulator = 0
self.onApply(target)
end
-- call this every frame (or heartbeat) with the real dt
function StatusEffect:tick(dt)
if not self._target then return end
-- advance timers
self._elapsed = self._elapsed + dt
self._tickAccumulator = self._tickAccumulator + dt
-- fire onTick once per tickDelay
while self._tickAccumulator >= self.TickDelay do
self._tickAccumulator = self._tickAccumulator - self.TickDelay
self.onTick(self._target, self.TickDelay)
end
-- expire?
if self._elapsed >= self.Duration then
self:expire(true)
end
end
-- expiredByTime = true if natural expiry, false if manually removed
function StatusEffect:expire(expiredByTime)
if not self._target then return end
self.onExpire(self._target, expiredByTime)
self._target = nil
end
return StatusEffect
User
script.Parent.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
local char = hit.Parent
if game.Players:GetPlayerFromCharacter(char) then
local plr = game.Players:GetPlayerFromCharacter(char)
local effected = {Name = plr.Name, Humanoid = char:FindFirstChild("Humanoid")}
Ticker.add(effected, burn)
end
end
end)
add a simple debounce
Assistant
Here’s a quick way to slap a per-player debounce on your touch handler so you don’t spam Ticker.add every frame.
Paste this at the top of your script:
lua
-- Simple per-player cooldown table
local touchDebounce = {} -- [player.UserId] = true while cooling down
local COOLDOWN = 1 -- seconds between re-triggers
Then your Touched connection becomes:
lua
local Players = game:GetService("Players")
local Ticker = require(…)
local burn = require(…) -- your status effect
script.Parent.Touched:Connect(function(hit)
local char = hit.Parent
local humanoid = char:FindFirstChild("Humanoid")
if not humanoid then return end
local plr = Players:GetPlayerFromCharacter(char)
if not plr then return end
local id = plr.UserId
if touchDebounce[id] then
return -- still cooling down
end
-- mark as in cooldown
touchDebounce[id] = true
task.delay(COOLDOWN, function()
touchDebounce[id] = nil
end)
-- apply your effect
local effected = {
Name = plr.Name,
Humanoid = humanoid
}
Ticker.add(effected, burn)
end)
How it works:
We keep touchDebounce[userId] set to true for COOLDOWN seconds.
Further touches from the same player during that window will early-exit.
After task.delay, we clear the debounce so they can be hit again.
Feel free to tweak COOLDOWN or scope the debounce table however you like!
What changed?
• Added a TickDelay parameter (defaults to 1s)
• Kept an internal _tickAccumulator so you only call onTick after each chunk of TickDelay has passed
• The outer ticker still drives .tick(dt) every frame
–––
2) Simplify your ticker loop
You no longer need task.wait(1) in your Heartbeat handler. Just call the update every frame—each effect will throttle itself:
lua
-- EffectTicker.lua (same as before, just ensure it calls effect:tick(dt))
local EffectTicker = {}
local active = {}
function EffectTicker.add(target, effect)
active[target] = active[target] or {}
active[target][effect] = true
effect:apply(target)
end
function EffectTicker.update(dt)
for target, effects in pairs(active) do
for effect in pairs(effects) do
effect:tick(dt)
if not effect._target then
effects[effect] = nil
end
end
if next(effects) == nil then
active[target] = nil
end
end
end
return EffectTicker
…and hook it up:
lua
local RunService = game:GetService("RunService")
local EffectTicker = require(script.EffectTicker)
RunService.Heartbeat:Connect(function(dt)
EffectTicker.update(dt)
end)
–––
3) Usage example
lua
local StatusEffect = require(script.StatusEffect)
local EffectTicker = require(script.EffectTicker)
-- a “Burn” that ticks every 0.5s, lasts 5s
local burn = StatusEffect.new(
"Burn",
5, -- total duration
0.5, -- tick every 0.5 seconds
function(t) print(t.Name, "starts burning!") end,
function(t, tick) print(t.Name, "takes burn damage (dt="..tick..")") end,
function(t, byTime) print(t.Name, "burn ended:", byTime and "timeout" or "dispell") end,
false -- not stackable
)
local dummy = { Name = "Dummy" }
EffectTicker.add(dummy, burn)
Now your onTick will fire exactly every 0.5 seconds, even though the ticker is updating each frame. 🎉