Weapon keeps shooting yourself and itself

Hello, I am currently scripting a rocket launcher, and when it shoots it explodes on yourself. Here is the code.

local replicatedStorage = game:GetService("ReplicatedStorage")
local teams = game:GetService("Teams")

replicatedStorage.Events.RocketFire.OnServerEvent:Connect(function(player, muzzlePosition, mousePosition, ammo, damage, headshotDamage, fireSoundId, reloadSoundId, explosionSoundId, hapticsStrength1, hapticsStrength2, hapticsType, hapticsTime)
	if player.Character then
		local character = player.Character
		local humanoid = character:WaitForChild("Humanoid")
		local tool = character:FindFirstChildWhichIsA("Tool")
		
		for i, v in pairs(player.Character.UpperTorso:GetChildren()) do
			if v.Name == "FireSound" then
				v:Destroy()
			end
		end
		
		for i, v in pairs(tool:WaitForChild(ammo):GetChildren()) do
			if v:IsA("MeshPart") then
				v.Transparency = 1
			end
		end
		
		local function createAmmo()
			local redRocket = replicatedStorage.Assets:WaitForChild("RedRocket"):Clone()
			local blueRocket = replicatedStorage.Assets:WaitForChild("BlueRocket"):Clone()
			local ammoPart
			local primaryPart
			local attachment = Instance.new("Attachment")
			local linearVelocity = Instance.new("LinearVelocity")
			local direction = (mousePosition - muzzlePosition.Position).unit
			
			if player.Team.Name == "Blue Battlers" then

				ammoPart = blueRocket
				primaryPart = ammoPart.PrimaryPart
			else

				ammoPart = redRocket
				primaryPart = ammoPart.PrimaryPart
			end
			
			attachment.Parent = primaryPart
			attachment.Position = primaryPart.CenterOfMass
			
			linearVelocity.Attachment0 = attachment
			linearVelocity.MaxForce = math.huge
			linearVelocity.VectorVelocity = direction * 250
			linearVelocity.Parent = primaryPart
			
			ammoPart.Parent = workspace.Debris
			ammoPart:PivotTo(muzzlePosition.CFrame)
			
			primaryPart.Touched:Connect(function(hit)
				
				if hit.Parent ~= tool.Parent or tool then
					
					local explosion = Instance.new("Explosion")
					explosion.Parent = workspace
					explosion.Position = hit.Position
				end
			end)
			
			task.wait(3)
			ammoPart:Destroy()
		end

		local fireSound = Instance.new("Sound")
		fireSound.Parent = player.Character.UpperTorso
		fireSound.Name = "FireSound"
		fireSound.Volume = 1
		fireSound.SoundId = fireSoundId
		
		fireSound:Play()
		
		createAmmo()
		
		repeat task.wait() until fireSound.IsPlaying == false
		
		local reloadSound = Instance.new("Sound")
		reloadSound.Parent = player.Character.UpperTorso
		reloadSound.Name = "ReloadSound"
		reloadSound.Volume = 1
		reloadSound.SoundId = reloadSoundId
		
		reloadSound:Play()
		
		for i, v in pairs(tool:WaitForChild(ammo):GetChildren()) do
			if v:IsA("MeshPart") then
				v.Transparency = 0
			end
		end
		
		repeat task.wait() until reloadSound.IsPlaying == false
		
		fireSound:Destroy()
		reloadSound:Destroy()
	end
end)
2 Likes

You should judge if hit.parent is player’character when primary part touched

if hit.parent and hit.parent:findfirstchild(“Humanoid”) and hit.parent ~= player.character

some accessory also can touch,so you also should judge hit.parent.parent if player’character.

This is not correct logic, I think you might have meant:

if hit.Parent ~= tool.Parent or hit.Parent ~= tool then

But that’s kinda unrelated to the issue, instead use this:

hitModel = hit:FindFirstAnscestor(player.Name)
if not hitModel then
   print("The part that was hit is not a child of the character or tool")
end

Sometimes the player could unequip the tool quick enough for the character to not register in the code.

local replicatedStorage = game:GetService("ReplicatedStorage")
local teams = game:GetService("Teams")

replicatedStorage.Events.RocketFire.OnServerEvent:Connect(function(player, muzzlePosition, mousePosition, ammo, damage, headshotDamage, fireSoundId, reloadSoundId, explosionSoundId, hapticsStrength1, hapticsStrength2, hapticsType, hapticsTime)
	if player.Character then
		local character = player.Character
		local humanoid = character:WaitForChild("Humanoid")
		local tool = character:FindFirstChildWhichIsA("Tool")
		
		for i, v in pairs(player.Character.UpperTorso:GetChildren()) do
			if v.Name == "FireSound" then
				v:Destroy()
			end
		end
		
		for i, v in pairs(tool:WaitForChild(ammo):GetChildren()) do
			if v:IsA("MeshPart") then
			  v.Transparency = 1
			end
		end
		
		local function createAmmo()
			local redRocket = replicatedStorage.Assets:WaitForChild("RedRocket"):Clone()
			local blueRocket = replicatedStorage.Assets:WaitForChild("BlueRocket"):Clone()
			local ammoPart
			local primaryPart
			local attachment = Instance.new("Attachment")
			local linearVelocity = Instance.new("LinearVelocity")
			local direction = (mousePosition - muzzlePosition.Position).unit
			
			if player.Team.Name == "Blue Battlers" then

				ammoPart = blueRocket
				primaryPart = ammoPart.PrimaryPart
			else

				ammoPart = redRocket
				primaryPart = ammoPart.PrimaryPart
			end
			
			attachment.Parent = primaryPart
			attachment.Position = primaryPart.CenterOfMass
			
			linearVelocity.Attachment0 = attachment
			linearVelocity.MaxForce = math.huge
			linearVelocity.VectorVelocity = direction * 250
			linearVelocity.Parent = primaryPart
			
			ammoPart.Parent = workspace.Debris
			ammoPart:PivotTo(muzzlePosition.CFrame)
			
			primaryPart.Touched:Connect(function(hit)
			  local char
			  if hit.Parent:IsA("Model") and hit.Parent:FindFirstChild("Humanoid") then
			    char = tool.Parent
			  else
			    char = tool.Parent.Parent.Character
			  end
				
				if hit.Parent ~= char then
					
					local explosion = Instance.new("Explosion")
					explosion.Parent = workspace
					explosion.Position = hit.Position
				end
			end)
			
			task.wait(3)
			ammoPart:Destroy()
		end

		local fireSound = Instance.new("Sound")
		fireSound.Parent = player.Character.UpperTorso
		fireSound.Name = "FireSound"
		fireSound.Volume = 1
		fireSound.SoundId = fireSoundId
		
		fireSound:Play()
		
		createAmmo()
		
		repeat task.wait() until fireSound.IsPlaying == false
		
		local reloadSound = Instance.new("Sound")
		reloadSound.Parent = player.Character.UpperTorso
		reloadSound.Name = "ReloadSound"
		reloadSound.Volume = 1
		reloadSound.SoundId = reloadSoundId
		
		reloadSound:Play()
		
		for i, v in pairs(tool:WaitForChild(ammo):GetChildren()) do
			if v:IsA("MeshPart") then
				v.Transparency = 0
			end
		end
		
		repeat task.wait() until reloadSound.IsPlaying == false
		
		fireSound:Destroy()
		reloadSound:Destroy()
	end
end)