Anti Team Kill for a New Home Defense game

So today, i’m making a Home Defense type game and i was wondering, How am i supposed to make an anti-team kill ?

I’ve basically tried everything, but none of them worked, so this is my last resort on getting the Anti-Team kill on my guns.

Here are the codes

Handler

local character
local humanoid
local animator
local animate
local animate2
local equipped = false
local filterArray = {}
local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist

damage = 17

script.Parent.RequestFire.OnServerEvent:Connect(function(plr,targetPosition)
	if script.Parent:FindFirstChild("Handle") and equipped == true then
		if animate2 then
			animate2:Stop()
		end
		animate2 = animator:LoadAnimation(script.Fire)
		animate2:Play()

		local spreadPosition = targetPosition + Vector3.new(math.random(-500,500)/1250,math.random(-500,500)/1250,math.random(-500,500)/1250)

		local raycastResult = workspace:Raycast(
			(script.Parent.Handle.CFrame * CFrame.new(-1.9,0.11,0)).Position,
			(spreadPosition - (script.Parent.Handle.CFrame * CFrame.new(-1.9,0.11,0)).Position).Unit*500,
			raycastParams)

		if raycastResult then
			local partHit = raycastResult.Instance
			if partHit.Parent:FindFirstChild("Humanoid") then
				if partHit.Parent.Humanoid.Health > 0 then
					if partHit.Name == "Head" then
						partHit.Parent.Humanoid:TakeDamage(damage*2.5)
					else
						partHit.Parent.Humanoid:TakeDamage(damage)
					end
					if partHit.Parent:FindFirstChild("Burn") == nil then
						local burn = script.Burn:Clone()
						burn.Parent = partHit.Parent
						burn.Disabled = false
					else
						partHit.Parent:FindFirstChild("Burn").BurningTime.Value = partHit.Parent:FindFirstChild("Burn").BurningTime.Value + 2
					end
				end
			elseif partHit.Parent:IsA("Accessory") then
				for _, ins in pairs(partHit.Parent.Parent:GetChildren()) do
					if ins:IsA("Accessory") then
						table.insert(filterArray,ins)
					end
				end
				raycastParams.FilterDescendantsInstances = filterArray
			end
		end
		coroutine.wrap(function()
			script.Parent.Handle.PointLight.Enabled = true
			script.Parent.Handle.BillboardGui.Enabled = true
			script.Parent.Rpart.FlashGui.Enabled = true
			wait(.08)
			script.Parent.Handle.PointLight.Enabled = false
			script.Parent.Handle.BillboardGui.Enabled = false
			script.Parent.Rpart.FlashGui.Enabled = false
		end)()
		local trailPart = Instance.new("Part",workspace)
		trailPart.Name = "TrailPart"
		trailPart.Transparency = 1
		trailPart.CanCollide = false
		trailPart.Anchored = true
		local attachment1 = Instance.new("Attachment",trailPart)
		local beam = Instance.new("Beam",trailPart)
		game.Debris:AddItem(attachment1,.075)
		game.Debris:AddItem(trailPart,.075)
		attachment1.WorldPosition = spreadPosition
		beam.Color = ColorSequence.new(Color3.fromRGB(255, 255, 127),Color3.fromRGB(255, 255, 127))
		beam.LightEmission = 1
		beam.LightInfluence = 0
		beam.Transparency = NumberSequence.new(0)
		beam.Attachment0 = script.Parent.Handle.BarrelAttachment
		beam.Attachment1 = attachment1
		beam.FaceCamera = true
		beam.Width0 = 0.002
		beam.Width1 = 0.1
		script.Parent.Handle.Fire1.PlaybackSpeed = 1
		script.Parent.Handle.Fire1:Play()
		script.Parent.Handle.Fire2.PlaybackSpeed = 1
		script.Parent.Handle.Fire2:Play()
	end
end)
script.Parent.Activated:Connect(function()
	script.Parent.Activated:Connect(function()
		for _, ins in pairs(workspace:GetChildren()) do
			if ins:FindFirstChild("Humanoid") then
				for _, ins2 in pairs(ins:GetChildren()) do
					if ins2:IsA("Accessory") then
						if table.find(filterArray,ins2) == nil then
							table.insert(filterArray,ins2)
						end
					end
				end
			end
		end
		raycastParams.FilterDescendantsInstances = filterArray
	end)
end)
script.Parent.Equipped:Connect(function()
	equipped = true
	character = script.Parent.Parent
	if table.find(filterArray,script.Parent.Parent) == nil then
		table.insert(filterArray,script.Parent.Parent)
	end
	if table.find(filterArray,script.Parent) == nil then
		table.insert(filterArray,script.Parent)
	end
	raycastParams.FilterDescendantsInstances = filterArray
	if character then
		humanoid = character.Humanoid
		if humanoid:FindFirstChild("Animator") == nil then
			animator = Instance.new("Animator",humanoid)
		elseif humanoid:FindFirstChild("Animator") then
			animator = humanoid:FindFirstChild("Animator")
		end
	end
	animate = animator:LoadAnimation(script.Hold)
	animate:Play()
end)
script.Parent.Unequipped:Connect(function()
	equipped = false
	if script.Parent:FindFirstChild("Handle") then
		script.Parent.Handle.PointLight.Enabled = false
		script.Parent.Handle.BillboardGui.Enabled = false
	end
	for _, ins in pairs(script.Parent.Handle:GetChildren()) do
		if ins:IsA("Sound") then
			ins:Stop()
		elseif ins:IsA("PointLight") then
			ins.Enabled = false
		end
	end
	if animate then
		animate:Stop()
	end
	if animate2 then
		animate2:Stop()
	end
end)

LocalHandler

local mouse = game.Players.LocalPlayer:GetMouse()
local equipped = false
local connection
local connection2
local active = false
local debounce = false
script.Parent.Equipped:Connect(function()
	equipped = true
	connection = mouse.Button1Down:Connect(function()
		if equipped == true and debounce == false then
			local Old = game.Players.LocalPlayer.Character.Humanoid.WalkSpeed
			game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 4
			local camera = game.Workspace.CurrentCamera
			active = true
			repeat
				if debounce == false then
					debounce = true
					local mousePos = mouse.Hit.Position
					script.Parent.RequestFire:FireServer(mousePos)
					camera.CFrame = camera.CFrame * CFrame.Angles(0.01,math.rad(0),0)
				end
				wait(0.07)
				debounce = false
			until active == false
		end
	end)
	connection2 = mouse.Button1Up:Connect(function()
		if equipped == true then
			active = false
			game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 16
		end
	end)
end)
script.Parent.Unequipped:Connect(function()
	equipped = false
	active = false
	connection:Disconnect()
	connection2:Disconnect()
end)

If you know how to do it, please reply.

hello i am calling from ind… i mean nyc here to help
here is script, it should work

local raycastResult = workspace:Raycast(
			(script.Parent.Handle.CFrame * CFrame.new(-1.9,0.11,0)).Position,
			(spreadPosition - (script.Parent.Handle.CFrame * CFrame.new(-1.9,0.11,0)).Position).Unit*500,
			raycastParams)

		if raycastResult then
			local players_serv = game:GetPlayers() --service for getting players ,i saw others using it so why not use it too
            local player = players_serv:GetPlayerFromCharacter(raycastResult.Instance:FindFirstAncestorOfClass("Model")) --getting the player model from whatever part of the player model this thing hit
            local team = player.Team
			if team ~= plr.Team then --if the team of the player that we hit is different from our player's team

					--enemy spotted, firing at target

			end

		end

Alternatively you could try plr.TeamColor instead of Team, but you need the BrickColor value

You can add tags to your teammates using collectionservice, then check to see if the target player has the team’s tag before handling damage on the server.