Explosion still dealing damage even after being destroyed

Hello!

Currently running into a problem creating an explosion module. I have a linger effect for the explosion to destroy itself after that linger time is over, and the module spawns a function to do damage over time.

The damage over time works great, the linger, not so much.
https://gyazo.com/2197715093fb14aaada9e80431efa617

In this clip, although I didn’t show you, the explosion does destroy itself after the given linger time, but apparently its radius stays, although the part itself doesn’t exist.

Here’s the code that handles creating explosions. The ‘findValue’ function is the exact same as using table.find:

function module.createExplosion(position : Vector3, damage : number, size : Vector3, tickRate : number, linger : number)
	-- Creates an explosion at a given distance, dealing a set amount of damage every tick until the explosion dissipates.
	local newExplosion = explosion:Clone()
	newExplosion.Position = position
	local sound = newExplosion:FindFirstChild('Explosion')
	local particle = newExplosion.Attachment:FindFirstChild('ParticleEmitter')
	
	newExplosion.Parent = workspace
	tweenService:Create(newExplosion, TweenInfo.new(linger, Enum.EasingStyle.Linear), {Size = size}):Play()
	tweenService:Create(newExplosion, TweenInfo.new(linger, Enum.EasingStyle.Linear), {Transparency = 1}):Play()
	sound:Play()
	particle:Clear()
	task.wait(0.01)
	particle:Emit(75)
	
	task.spawn(function()
		while task.wait(tickRate) do
			print('doing the thing!')
			local touched = {}
			print(touchedPlayers)

			local touchedParts = workspace:GetPartsInPart(newExplosion)
			for _, part in touchedParts do
				print('has touched parts!')
				if part.Parent:FindFirstChildOfClass('Humanoid') and game.Players:GetPlayerFromCharacter(part.Parent) and not findValue(touched, part.Parent) then
					print('is a player, damaging')
					local humanoid = part.Parent:FindFirstChildOfClass('Humanoid')
					table.insert(touched, part.Parent)
					humanoid:TakeDamage(damage)
				end
			end
		end
	end)
	game:GetService('Debris'):AddItem(newExplosion, linger)
end

Any help would be huge!

Was able to fix this by changing the spawned function into a coroutine and closing it after the linger effect is over. If there are any better solutions, please feel free to drop them here. Not sure why I didn’t do this sooner.

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