Subspace Tripmine Chain Reaction Causes Game Lag

So I made a custom script for a new subspace tripmine bomb for my game, and overtime me and a friend decided to make it so that the subspace tripmines have a chain reaction whenever they hit another subspace tripmine that is owned by the same player, this makes a chain reaction and causes all of the bombs to explode.

BUT, I’m having an issue with programming this because for some reason the moment a player on a different team touches the subspace tripmine, the game lags for a long time.

Here’s the script, I’m still not sure why it does that.

--\\ Variables //--
local tool = script.Parent
local bombHandle = tool:WaitForChild("Handle")
local ActivateBombRemote = script.Parent:WaitForChild("ActivateBomb")

local tickSound = Instance.new("Sound")
tickSound.SoundId = "http://www.roblox.com/asset/?id=11956590"
tickSound.Parent = tool

local explosionSound = Instance.new("Sound")
explosionSound.SoundId = "http://www.roblox.com/asset/?id=11984351"
explosionSound.Parent = tool

local blastRegion3L = 17/2 -- -- The Length of the Blast (Dividing all of the dimensions by 2 so that the region3 is not twice its size after making its size relative to the position)
local blastRegion3W = 17/2 -- The Width of the Blast
local blastRegion3H = 12/2 -- The Maximum height of the region3

local baseDamage = 60
--\\ Functions //--
function explosionParticles(bomb)
	print(bomb)
	local explosionClone = bomb.Explosion:Clone()
	explosionClone.Parent = bomb
	explosionClone.Enabled = true

	return explosionClone
end

function explode(bomb, user)
	local tween = game.TweenService:Create(bomb, TweenInfo.new(.5, Enum.EasingStyle.Linear, Enum.EasingDirection.In, 0, false, 0), {
		Transparency = 0
	})
	
	bomb.Anchored = true -- Anchores the bomb so that it doesn't go elsewhere due to the explosion.

	local explosionParticle = explosionParticles(bomb)

	explosionSound:Play()

	bomb.Transparency = 1

	local region3 = Region3.new(
		bomb.Position - Vector3.new(blastRegion3L, 1, blastRegion3W),
		bomb.Position + Vector3.new(blastRegion3L, blastRegion3H, blastRegion3W) -- Makes a square that is 5x5 long and wide, and 10 studs high (hence the 1 to 10)
	) -- Creates a region3 that destroys any parts in the explosions radius.

	local region3Visual = Instance.new("Part") -- The visualized part for the region3.
	region3Visual.Name = "blastRadiousVisual"
	region3Visual.CanCollide = false
	region3Visual.Size = region3.Size
	region3Visual.CFrame = region3.CFrame
	region3Visual.Anchored = true
	region3Visual.Transparency = .5
	region3Visual.Parent = workspace.Terrain
	region3Visual.BrickColor = BrickColor.new("Persimmon")

	local explosion = Instance.new("Explosion") -- The explosion instance
	explosion.Parent = bomb
	explosion.Position = bomb.Position
	explosion.BlastRadius = 0
	
	explosion.DestroyJointRadiusPercent = 0
	explosion.BlastPressure = 900000

	explosion.BlastRadius = blastRegion3L

	local overlapParams = OverlapParams.new()
	overlapParams.FilterDescendantsInstances = {tool, tool.Handle, region3Visual}

	local parts = workspace:GetPartBoundsInBox(region3.CFrame, region3.Size, overlapParams)

	explosion.Hit:Connect(function(Part)
		if Part.Name == "HumanoidRootPart" then
			local character = Part.Parent
			local player = game.Players:GetPlayerFromCharacter(character)

			if player and player.Team ~= user.Team then
				character:FindFirstChildOfClass("Humanoid"):TakeDamage(baseDamage)
				
				if character:FindFirstChildOfClass("Humanoid").Health == 0 then
					user.leaderstats.Kills.Value += 1
				end
			end
		end
	end)

	for i, v in pairs(parts) do
		if v.Name == "Brick" then
			v.Anchored = false

			v.CanTouch = false

			v.BrickColor = BrickColor.new("Hot pink")
			
			
			for i, constraint in pairs(v:GetChildren()) do
				if constraint:IsA("Snap") or constraint:IsA("Weld") or constraint:IsA("WeldConstraint") then
					constraint:Destroy()
				end
			end

			task.delay(2, game.Destroy, v)
		end

		if v.Name == "GameBrick" then v.Name = "TaggedGameBrick" -- TaggedGameBricks are GameBricks in the game that have been effected by the bomb, while regular Bricks are just bricks you can destroy by default.
			v.Anchored = false

			v.CanTouch = false

			user.leaderstats.Bricks.Value += 1 -- The total bricks the player has broken throughout the round.
			user["T. Bricks"].Value += 1 -- The total bricks that the player has destroyed throughout there playtime
			user.leaderstats.Studs.Value += .1 -- Increases the players currency (Studs) by 0.1.
			v.BrickColor = BrickColor.new("Hot pink")

			task.delay(2, game.Destroy, v) -- later deletes the part.
		end
		
		if v.Name == "SubspaceTripmine" then
			explode(v, user) -- Causes the chain reaction
		end
	end

	task.delay(3, game.Destroy, bomb)
	task.delay(3, game.Destroy, region3Visual)

	task.wait(2)

	explosionParticle.Enabled = false -- Disables the explosion particles.
end

local Connection -- Makes the bomb explode (function)
ActivateBombRemote.OnServerEvent:Connect(function(user) -- This is the function that makes the bomb go off, which connects the ActivateBomb Remote Event to this script after it gets activated by the local script.
	local bombClone = bombHandle:Clone()
	bombClone.Parent = workspace.Bombs
	bombClone.CanCollide = true
	bombClone.CFrame = bombHandle.CFrame * CFrame.new(0, 2, 2)
	bombClone.Name = "SubspaceTripmine"

	local tween = game.TweenService:Create(bombClone, TweenInfo.new(.5, Enum.EasingStyle.Linear, Enum.EasingDirection.In, 0, false, 0), {
		Transparency = 1
	})

	tween:Play()

	tickSound:Play()

	tickSound.Ended:Wait()

	Connection = bombClone.Touched:Connect(function(Hit)
		local Player = game:GetService("Players"):GetPlayerFromCharacter(Hit.Parent)
		if Player and Player.Team ~= user.Team then
			Connection:Disconnect() -- Assuming bombClone gets destroyed, this isn't necessary as destroying ANY instance also DROPS any event.
			explode(bombClone, user)
		end
	end)
	
	task.wait(60)
	
	if Connection.Connected then
		explode(bombClone, user)
	end
end)

------------------------------------------------------------------------------------------

1 Like

Instead of checking if the part name is HumanoidRootPart, try checking if the part’s parent is a player.

1 Like
if v.Name == "SubspaceTripmine" then
	task.spawn(explode, v, user) -- Causes the chain reaction
end
1 Like

This still makes the game have a heavy amount of lag on every player in the game

1 Like