My event threads are disappearing?

This script runs up to 20 times per second as there is a spell in the game that casts 20 spikes, each applying heal to the casting player when in contact with the opponent.

The particle effect needs to appear once and then disappear after 2 seconds max, shown in the code.

The particle effect doesn’t get generated only once like it should. Instead, it spawns like 10 times and some of these particles are never erased from the game. This only happens when casting this spell and having multiple of this spell. It doesn’t happen always either. I replicate this by moving on the spikes.The damage calculations are all correct, its only this segment of the code with the heal that behaves incorrectly.

function CombatManager.Heal(humanoid, playerCharacter, duration, value)
	local particleExists = false
	local activeParticle = humanoid.Parent.LowerTorso:FindFirstChild("Heal")
	if (activeParticle and activeParticle.ParticleEmitter.Enabled) then
		particleExists = true
	end
	
	local particle
	if not particleExists then
		particle = game:GetService("ServerStorage").Particles.Heal:Clone()
		particle.Parent = playerCharacter.LowerTorso
		particle.CFrame = playerCharacter.LowerTorso.CFrame
		particle.WeldConstraint.Part1 = playerCharacter.LowerTorso
	end
	
	local timer = 0
	local dummyCheck = humanoid.Parent:FindFirstChild("TargetDummyA040Clr")
	StatusUIDisplay:FireClient(game.Players:FindFirstChild(humanoid.Parent.Name), "heal", true)
	humanoid.Parent.Stats.Frame.Heal.Visible = true
	
	if(duration == 0) then
		humanoid.Health = math.min(humanoid.MaxHealth, humanoid.Health + value)
		
	else
		local healTick = value / 30
		while (timer < duration) do
			humanoid.Health = math.min(humanoid.MaxHealth, humanoid.Health + healTick)
			
			local timeTick = wait()
			healTick = value * timeTick
			timer = timer + timeTick
			
			StatusUIDisplay:FireClient(game.Players:FindFirstChild(humanoid.Parent.Name), "heal", true)
			humanoid.Parent.Stats.Frame.Heal.Visible = true
		end
	end
	
	wait(.5)
	StatusUIDisplay:FireClient(game.Players:FindFirstChild(humanoid.Parent.Name), "heal", false)
	
	if not particleExists then
		particle.ParticleEmitter.Enabled = false
	end
	
	humanoid.Parent.Stats.Frame.Heal.Visible = false
	wait(1.5)
	if not particleExists then
		print("destroy")
		particle:Destroy()
	end
end

Sorry for the messy code. This script runs in the server as well as everything in the execution path.
Even if there is a debounce issue that causes multiple particles to spawn, each variable of particleExists is local and they should eventually get destroyed. I can’t figure out what is wrong here. Thank you.

1 Like
  1. To emit an amount of particles of the ParticleEmitter use ParticleEmitter:Emit(),
    1.1 I suggest using this instead of Enabling the Particle Emitter if you want to spawn multiple Particles at the same time.
  2. Is “particle” a ParticleEmitter or a Part?
  3. I suggest instead of wait() and then particle:Destroy() to use Debris:AddItem()
    3.1. This should make it so that the “particle” gets destroyed.
  4. Give us more information next time, it’s highly appreciated :smiley: ; such as:
    4.1. Information about what you’re cloning (like a screenshot of it and it’s children).
    4.2. A video of what goes wrong.
    4.3. An error if is there.

Thank you for responding.

ParticleEmitter:Emit() sounds really nice, I was looking for such a thing on the gui menu similar to bursts in Unity Engine but didn’t find it. the “particle” is an invisible Part containing the particle emitter. image the weld is to attach to the torso.

Debris:AddItem() is something I use from time to time, is there a difference in this case against :Destroy() ?

Also, I feel like generating each particle emitter once during CharacterAdded and cloning into the character, then using this script to call Emit() sounds like an alternate option that doesn’t involve creating new clones of this Part. Do you know anything about the possible performance impact of a disabled particle emitter in the scene? I would assume there are no render calls and no impacts but would like to make sure.

edit: there are no errors with the script whatsoever, which is what confused me in the first place and made me believe the thread just vanishes.

  1. It’s better than just using a wait() and then a destroy and it should fix many of those healing particles if you’d just run that
  2. What you’re doing at the first if is: checking if there’s another activeParticle and then setting particleExists true, note that you’re not setting particle to anything there, as you later use it.
  3. I suggest instead of using particleExists
    3.1. if not particleExists then instead use: if not particle then (at the start)
    3.2 if particleExists then instead use: if particle then (at the end)
  4. and
if not particleExists then
	particle.ParticleEmitter.Enabled = false
end

wouldn’t work at all, this is fr a mess off code, you don’t even set particleExists = true at:

if not particleExists then
	particle = game:GetService("ServerStorage").Particles.Heal:Clone()
	particle.Parent = playerCharacter.LowerTorso
	particle.CFrame = playerCharacter.LowerTorso.CFrame
	particle.WeldConstraint.Part1 = playerCharacter.LowerTorso
end
  1. I am confused about: as you make a healing frame dissapear and then you wait before you destroy it?
humanoid.Parent.Stats.Frame.Heal.Visible = false
wait(1.5)
if not particleExists then
	print("destroy")
	particle:Destroy()
end

A video would be highly appreciated so I know what the frame is for example, etc!

Sorry for not responding earlier. The part with the particleExists has nothing wrong with it, it gets set in the first few lines of the code, you didn’t see that. The Frame part is a BillboardGui and that part is also fine. The issue, somewhat shamefully was a Debris call that destroyed the spell projectiles after 6 seconds in another script. The thread would essentially be killed while yielded and the particles would remain. Now it is fixed. Sorry for wasting time.

edit: I will be more descriptive in the further posts I make on this forum. I realize that is an issue right now.