How to prevent bullet hit the gun component itself

heres what happened: 2022-01-23 22-44-39
please help me solve this problem

I used basepart for the bullet idk how can I fix this

server

game.ReplicatedStorage.Events.Fire.OnServerEvent:Connect(function(player, mousePos, pos, BulletSpeed, rotate, sound)
	local bullet = Instance.new("Part")
	bullet.Position = pos
	bullet.CanCollide = false
	bullet.Size = Vector3.new(0.2, 0.2, 2.5)
	bullet.Name = "bullet"
	bullet.Orientation = rotate
	bullet.Color = Color3.fromRGB(255, 89, 89)
	bullet.Material = Enum.Material.Neon
	bullet.Transparency = 0
	bullet.CastShadow = false
	bullet.Parent = workspace.Terrain
	
	local function onPartTouch(otherPart)
		local partParent = otherPart.Parent
		local humanoid = partParent:FindFirstChildWhichIsA("Humanoid")
		if humanoid and not humanoid:IsDescendantOf(player.Character) then
			local tag = humanoid:FindFirstChild("creator") or Instance.new("ObjectValue")
			tag.Name = "creator"
			tag.Value = player
			tag.Parent = humanoid
			debris:AddItem(tag, .5)
			if otherPart.Name == "Head" then
				humanoid.Health = humanoid.Health -70
			else
				humanoid.Health = humanoid.Health -1
			end
			
		else
			ParticleEmitter:emit(ParticleEmitter.particleType.electricSparks, bullet.CFrame, 60, 5, 0.1, 3, true)
			bullet:Destroy()
		end
		if (humanoid.Health <= 0) then
			print("killed")
		end
	end
	bullet.Touched:Connect(onPartTouch)
```

client
```	
					local sound = "rbxassetid://"..EquippedGun_Module.firesound
						local pos = EquippedGun.FirePoint.Position
						local BulletSpeed = EquippedGun_Module.BulletSpeed
						local rotate = EquippedGun.FirePoint.Orientation
						RepStorage.Events.Fire:FireServer(Mouse.Hit.p,pos,BulletSpeed,rotate,sound)
```
if partParent == gun or partParent.Parent == gun or partParent .Parent.Parent == gun then
   return
end

Put this after the variable called partParent
Gun is the tool itself
Else you could do

do
   local currentParent = partParent
   repeat
      currentParent = currentParent.Parent
      
   until (currentParent == gun or currentParent == workspace or currentParent == nil)

   if currentParent ~= gun then return end
end
1 Like