How to improve explosion script

I have recently made a explosion script and it’s suppose to make bricks fly around but all it does right now is destroy it’s welds no matter the power of the blast and only works if I put the instance in the loop which creates many explosion copies and then lags the game.

local module = {}

local replicatedStorage = game:GetService("ReplicatedStorage")
local collectionService = game:GetService("CollectionService")

local playerScores = replicatedStorage:FindFirstChild("PlayerScores")

local partsHit = {}

function module:Explode(playerName, position, radius)
	local explosion = Instance.new("Explosion")
	explosion.BlastPressure = 10000
	explosion.DestroyJointRadiusPercent = 0
	explosion.BlastRadius = radius
	explosion.Position = position
	explosion.Visible = false
	
	local hitConnection

	hitConnection = explosion.Hit:Connect(function(part, distance)
		if partsHit[part] then
			return
		end
	
		if collectionService:HasTag(part, "Destructible") and not collectionService:HasTag(part, "HitPart") then
			partsHit[part] = true
			part:BreakJoints()
			
			local total = playerScores:FindFirstChild(playerName)
			if not total then
				local value = Instance.new("IntValue")
				value.Name = playerName
				value.Value = 1
				value.Parent = playerScores
			else
				total.Value = total.Value + 1
				if playerName.Team == "Bright blue Team" then
					game.Workspace.BlueScore.Value = game.Workspace.BlueScore.Value + 1
				else
					game.Workspace.RedScore.Value = game.Workspace.RedScore.Value + 1
			end
			
			for i,v in pairs(part:GetTouchingParts()) do
				if collectionService:HasTag(v, "Destructible") then
					v:BreakJoints()
				end
			end
			
			
			delay(5, function()
				part:Destroy()
			end)
			
			collectionService:AddTag(part, "HitPart")
			end
		end
	end)

	local explosion2 = Instance.new("Explosion")
	explosion2.BlastPressure = 100000
	explosion2.DestroyJointRadiusPercent = 0
	explosion2.BlastRadius = radius
	explosion2.Position = position
	explosion2.Parent = game.Workspace.Explosions
 
	explosion.Parent = workspace
	
	delay(0.25, function()
		hitConnection:Disconnect()
	end)
	end

return module
1 Like

I would post this in the code-review category Code Review - Developer Forum | Roblox

1 Like

I would remove the CollectionService CollectionService | Documentation - Roblox Creator Hub
“Tags are sets of strings applied to objects that replicate from the server to the client and in Team Create” Also check the micro profiler to see where the time is being used up.

2 Likes