Best way to clean up things like this?

Hey,

I’m making a round based game and the rounds have a lot of for loops with task.spawn’s. I want to know what the best of cleaning stuff like this if the player dies?

local Debris = game:GetService("Debris")
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerStorage = game:GetService("ServerStorage")
local TweenService = game:GetService("TweenService")

local Bombs = {
	Amount = 60;
	ExplodeTime = 2.5;
	ExplosionRadius = 5;

	["Medium"] = {
		AmountMultiplier = 1.5,
		ExplodeTimeDivision = 1.2,
		ExplosionRadiusMultiplier = 2,
	};

	["Hard"] = {
		AmountMultiplier = 1.7,
		ExplodeTimeDivision = 1.4,
		ExplosionRadiusMultiplier = 2.7,
	}
}

function Bombs.StartAsync(Difficulty: string)
	local Variables = require(ReplicatedStorage.Source.Shared.Variables)
	local Amount, ExplodeTime, ExplosionRadius = Bombs.Amount, Bombs.ExplodeTime, Bombs.ExplosionRadius
	local Bomb, Radius, Grass, Cache = ServerStorage.Assets.Bomb, ServerStorage.Assets.Radius, Variables.Paths.Map.Grass, Variables.Paths.Cache
	
	for i, _ in pairs(Bombs) do
		if i ~= Difficulty then continue end
		
		Amount = math.round(Bombs.Amount * Bombs[i].AmountMultiplier)
		ExplodeTime /= Bombs[i].ExplodeTimeDivision
		ExplosionRadius *= Bombs[i].ExplosionRadiusMultiplier
	end
	
	local size = Grass.Size / 1.2
	local x = size.X/2
	local z = size.Z/2
	
	-- scatter bombs
	for i = 1, Amount do
		local offset = Vector3.new(math.random(-x, x), 1.062, math.random(-z, z))
		local bomb = Bomb:Clone()
		
		local tweenBomb = TweenService:Create(bomb, TweenInfo.new(0.3, Enum.EasingStyle.Quad), {Transparency = 0})
		
		bomb.Transparency = 1
		bomb.Parent = Cache
		bomb.Position = (Grass.Position + offset)
			+ Vector3.new(0, math.abs(Grass.Position.Y), 0)
		bomb.Anchored = true
		
		-- explode
		task.spawn(function()
			bomb.Fuse:Play()

			local radius = Radius:Clone()
			radius.Transparency = 1
			radius.Size = Vector3.new(ExplosionRadius*2, ExplosionRadius*2, 0.001)
			radius.Position = Vector3.new(bomb.Position.X, 0.5, bomb.Position.Z)
			radius.Parent = bomb
			
			local tweenRadius = TweenService:Create(radius, TweenInfo.new(ExplodeTime, Enum.EasingStyle.Quad), {Transparency = 0.1})
			
			tweenRadius:Play()
			tweenBomb:Play()
			
			task.wait(ExplodeTime)
			
			if not bomb or not bomb.Fuse then return end
			
			bomb.Fuse:Stop()
			bomb.Explode:Play()

			local explosion = Instance.new("Explosion")
			explosion.BlastRadius = ExplosionRadius
			explosion.BlastPressure = 1000000
			explosion.DestroyJointRadiusPercent = 0.7
			explosion.Position = bomb.Position
			explosion.Parent = bomb
			
			explosion.Hit:Connect(function(part: BasePart, distance: number) 
				if Players:GetPlayerFromCharacter(part.Parent) and part.Parent.Humanoid.Health ~= 0 then
					part.Parent.Humanoid.Health = 0
				end
			end)
			bomb.CanCollide = false
			bomb.Transparency = 1
			
			radius:Destroy()
			Debris:AddItem(bomb, 5)
		end)

		task.wait(0.1)
		
		if i == Amount then
			task.wait(4)
		end
	end
end

function Bombs.Clean() -- clean bombs

end

return Bombs

Separate the function from the task.spawn and give it a name,
e.g: local function doSomething()
task.spawn(doSomething)

And when the player dies, do: task.cancel(doSomething()) which ends the thread.

1 Like

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