local function Y(Y,E,k)if Y and E then local E,L=FindCharacterAncestor(Y.Parent)if E then local Y=CreatorTag.Value if Y and not Y.Neutral then local k=PlayersService:GetPlayerFromCharacter(E)if k and(k~=Y and k.TeamColor==Rocket.BrickColor)then return end end end if L and L.Health>0 then if Y.Name=="\084\111\114\115\111"then ApplyTags(L)L:TakeDamage(BLAST_DAMAGE)end else if Y.Name~="\072\097\110\100\108\101"then Y:BreakJoints()local E=Instance.new("\066\111\100\121\070\111\114\099\101",Y)E.force=((Y.Position-k).unit*BLAST_FORCE)*Y:GetMass()DebrisService:AddItem(E,.1)end end end end local function E(E)if Rocket and E then if IGNORE_LIST[string.lower(E.Name)]then return end local k=CreatorTag.Value if k then if k.Character and k.Character:IsAncestorOf(E)then return end if not k.Neutral then local Y=FindCharacterAncestor(E.Parent)local L=PlayersService:GetPlayerFromCharacter(Y)if L and(L~=k and L.TeamColor==Rocket.BrickColor)then return end end end if E==game.Workspace.Terrain then local Y=Rocket.Position+Rocket.CFrame.lookVector*(Rocket.Size.Z/2)local E=game.Workspace.Terrain:WorldToCellPreferSolid(Y)local k=game.Workspace.Terrain:GetCell(E.X,E.Y,E.Z)if k==Enum.CellMaterial.Water or k==Enum.CellMaterial.Empty then return end end local L=Instance.new("\069\120\112\108\111\115\105\111\110")L.BlastPressure=0 L.BlastRadius=BLAST_RADIUS L.ExplosionType=Enum.ExplosionType.NoCraters L.Position=Rocket.Position L.Parent=game.Workspace L.Hit:connect(function(E,k)Y(E,k,L.Position)end)script.Parent=L CreatorTag.Parent=script Rocket:Destroy()end end

local function OnExplosionHit(hitPart, hitDistance, blastCenter)
	if hitPart and hitDistance then
		local character, humanoid = FindCharacterAncestor(hitPart.Parent)

		if character then
			local myPlayer = CreatorTag.Value
			if myPlayer and not myPlayer.Neutral then -- Ignore friendlies caught in the blast
				local player = PlayersService:GetPlayerFromCharacter(character)
				if player and player ~= myPlayer and player.TeamColor == Rocket.BrickColor then
					return
				end
			end
		end

		if humanoid and humanoid.Health > 0 then -- Humanoids are tagged and damaged
			if hitPart.Name == 'Torso' then
				ApplyTags(humanoid)
				humanoid:TakeDamage(BLAST_DAMAGE)
			end
		else -- Loose parts and dead parts are blasted
			if hitPart.Name ~= 'Handle' then
				hitPart:BreakJoints()
				local blastForce = Instance.new('BodyForce', hitPart) --NOTE: We will multiply by mass so bigger parts get blasted more
				blastForce.force = (hitPart.Position - blastCenter).unit * BLAST_FORCE * hitPart:GetMass()
				DebrisService:AddItem(blastForce, 0.1)
			end
		end
	end
end

local function OnTouched(otherPart)
	if Rocket and otherPart then
		-- Fly through anything in the ignore list
		if IGNORE_LIST[string.lower(otherPart.Name)] then
			return
		end

		local myPlayer = CreatorTag.Value
		if myPlayer then
			-- Fly through the creator
			if myPlayer.Character and myPlayer.Character:IsAncestorOf(otherPart) then
				return
			end

			 -- Fly through friendlies
			if not myPlayer.Neutral then
				local character = FindCharacterAncestor(otherPart.Parent)
				local player = PlayersService:GetPlayerFromCharacter(character)
				if player and player ~= myPlayer and player.TeamColor == Rocket.BrickColor then
					return
				end
			end
		end

		-- Fly through terrain water
		if otherPart == game.Workspace.Terrain then
			--NOTE: If the rocket is large, then the simplifications made here will cause it to fly through terrain in some cases
			local frontOfRocket = Rocket.Position + (Rocket.CFrame.lookVector * (Rocket.Size.Z / 2))
			local cellLocation = game.Workspace.Terrain:WorldToCellPreferSolid(frontOfRocket)
			local cellMaterial = game.Workspace.Terrain:GetCell(cellLocation.X, cellLocation.Y, cellLocation.Z)
			if cellMaterial == Enum.CellMaterial.Water or cellMaterial == Enum.CellMaterial.Empty then
				return
			end
		end

		-- Create the explosion
		local explosion = Instance.new('Explosion')
		explosion.BlastPressure = 0 -- Completely safe explosion
		explosion.BlastRadius = BLAST_RADIUS
		explosion.ExplosionType = Enum.ExplosionType.NoCraters
		explosion.Position = Rocket.Position
		explosion.Parent = game.Workspace

		-- Connect custom logic for the explosion
		explosion.Hit:connect(function(hitPart, hitDistance) OnExplosionHit(hitPart, hitDistance, explosion.Position) end)

		-- Move this script and the creator tag (so our custom logic can execute), then destroy the rocket
		script.Parent = explosion
		CreatorTag.Parent = script
		Rocket:Destroy()
	end
end
