Automatic weapon still teamkills?

I seem to be having another issue with one of my ranged weapon scripts. (Man, raycasting is hard… :woozy_face:)

Anyways- the issue here is that whenever a player fires with the weapon by holding down M1, they can still damage team members.

The problem though is that- whenever I try to fix the issue 1 of 2 things happen…
Players will never take damage
…or it’ll just go back to damaging everyone.

If anyone knows the solution, that would be greatly appreciated! :upside_down_face:

local ReplicatedStorage = game:GetService('ReplicatedStorage')
local remoteEvent = ReplicatedStorage:WaitForChild('RFFired')

local damage_amount = 8

local GRAVITY_ACCELERATION = workspace.Gravity

local Team = game.Teams


remoteEvent.OnServerEvent:Connect(function(player, gunPos, gunOr, mosPos)

	local bullet = Instance.new('Part')
	bullet.Name = 'Bullet'
	bullet.Parent = game.Workspace
	bullet.Shape = Enum.PartType.Block
	bullet.Transparency = 0.25
	bullet.Material = "Neon"
	bullet.Size = Vector3.new(0.35, 0.35, 0.35)
	bullet.BrickColor = BrickColor.new('Electric blue')
	bullet.CanCollide = false

	local speed = 150
	bullet.CFrame = CFrame.new(gunPos, mosPos)
	bullet.Velocity = bullet.CFrame.lookVector * speed
	
	local bodyForce = Instance.new('BodyForce', bullet)
	bodyForce.Name = 'Antigravity'
	bodyForce.Force = Vector3.new(0, bullet:GetMass() * GRAVITY_ACCELERATION, 0)
	
	bullet.Touched:Connect(function(otherPart)
		local humanoid = otherPart.Parent:FindFirstChild('Humanoid')
		if humanoid and humanoid.Parent.Name ~= player.Name then
			if humanoid.Health > 0 and humanoid.Health - damage_amount <= 0 then
				player.leaderstats.KOs.Value += 1

			end

			humanoid:TakeDamage(damage_amount)
			bullet:Destroy()
		end

	end)

	game:GetService('Debris'):AddItem(bullet, 0.5)

end)

Where are you doing your raycasting? Because there is a whitelist/blacklist feature you can use to add or exclude certain parts. You can use that to check if a player is on the opposite team and then damage them

1 Like

This is a serverscript.
I am aware of whitelisting and blacklisting but everytime I try to do it I end up with an error that just leaves me more confused in the end…