I’m attempting to create a simple “burn” function that takes the player, hit part of the body, and the enemy. Given these arguments, it applies damage to the target a total of 3 times and then destroys the particle effects. Despite being simple enough, it has proven to be anything but.
The function will randomly decide it’s had enough and just end itself midway through any part of it, even just at the beginning. This causes the particle effects to not be removed as the function was incomplete. I’ve tried using coroutines but that doesn’t seem to work either.
This is the troublesome code:
local function Burn(plr, hit, enemy)
local chr = plr.Character or plr.CharacterAdded:Wait()
local tool = chr:FindFirstChildOfClass("Tool")
if not enemy or not hit or not tool then return end
local tor = hit.Parent:FindFirstChild("UpperTorso")
if not hit.Parent or not tor then return end
if game.Players:GetPlayerFromCharacter(enemy.Parent) then return end
if tor:FindFirstChild("FEffect") then return end
local fireEffect = game.ReplicatedStorage.GameItems.Effects.Fire.FEffect:Clone()
fireEffect.Parent = tor
local fireLight = game.ReplicatedStorage.GameItems.Effects.Fire.FireLight:Clone()
fireLight.Parent = tor
local aBurn = game.ReplicatedStorage.GameItems.Effects.Fire.ABurn:Clone()
aBurn.Parent = tor
local dmg = require(tool.WeaponConfig).BurnDamage
local function applyBurn()
for i = 1, 3 do
if not enemy or enemy.Health <= 0 then break end
aBurn.Playing = false
aBurn.Playing = true
enemy:TakeDamage(dmg)
local ObjModel = CreateObject(hit, dmg, false)
DestroyTag(ObjModel)
ObjModel:Destroy()
end
fireEffect:Destroy()
fireLight:Destroy()
aBurn:Destroy()
end
coroutine.wrap(applyBurn)()
end
Around 50% of the time the code works as intended. Other times it will usually break after the first burn apply, not doing any more of them and leaving the target with the burn particles still on them. The CreateObject function creates damage numbers that disappear after a second. It contains a few wait()s, creating gaps of time between each burn. What could possibly be wrong with this code?