I need help with attacking NPC

local ATTACK_RANGE = 2.5
local CanAttack = false
local attacking = false
local Combo = 1
local DoingCombo = 0

local Hum = script.Parent:WaitForChild("Humanoid")
local FX = script:WaitForChild("FX")
local TweenService = game:GetService("TweenService")

local Anims = script.Parent:WaitForChild("Anims")
local Attack1Anim = Hum:LoadAnimation(Anims:WaitForChild("Attack1"))
local Attack2Anim = Hum:LoadAnimation(Anims:WaitForChild("Attack2"))
local Attack3Anim = Hum:LoadAnimation(Anims:WaitForChild("Attack3"))
local Attack4Anim = Hum:LoadAnimation(Anims:WaitForChild("Attack4"))

local HiteffectBall = function(Target,Pos)
	local ClonedBall = FX.Thing:Clone()
	ClonedBall.Parent = Target
	ClonedBall.CFrame = Pos
	ClonedBall.CFrame = CFrame.new(ClonedBall.Position, Target.Position)
	ClonedBall.Transparency = 0
	game.Debris:AddItem(ClonedBall,1)

	if DoingCombo == 4 then
		ClonedBall.BrickColor = BrickColor.new("Neon orange")
	end

	TweenService:Create(ClonedBall,TweenInfo.new(0.5,Enum.EasingStyle.Quad,Enum.EasingDirection.InOut),{CFrame = ClonedBall.CFrame + ClonedBall.CFrame.lookVector * -7,Transparency = 1,Size = Vector3.new(0.087, 0.08, 3.35)}):Play()
end

local InsertDisabled = function(Target,Time)
	local Disabled = Instance.new("BoolValue",Target)
	Disabled.Name = "Disabled"
	game.Debris:AddItem(Disabled,Time)
end	

local target = nil

local function runToTarget()
	local targetPosition = (script.Parent.HumanoidRootPart.Position - target.Position).Unit * (ATTACK_RANGE - 1) + target.Position

	script.Parent.Humanoid:MoveTo(targetPosition)
end

function findTargets()
	local list = game.Workspace:GetChildren()
	local torso = nil
	local dist = 25
	local temp = nil
	local human = nil
	local temp2 = nil
	for x = 1, #list do
		temp2 = list[x]
		if (temp2.className == "Model") and (temp2 ~= script.Parent) then
			temp = temp2:findFirstChild("HumanoidRootPart")
			human = temp2:findFirstChild("Humanoid")
			if (temp ~= nil) and (human ~= nil) and (human.Health > 0) then
				if (temp.Position - script.Parent.HumanoidRootPart.Position).magnitude < dist then
					torso = temp
					dist = (temp.Position - script.Parent.HumanoidRootPart.Position).magnitude
					target = torso.Parent.HumanoidRootPart
				else
					target = nil
				end
			end
		end
	end
end


local function onHeartbeat()
	
	if target and script.Parent:FindFirstChild("Disabled") == nil then
		if target.Parent.Humanoid.Health <= 0 then
			target = nil
		else
			
			local inAttackRange = (target.Position - script.Parent.HumanoidRootPart.Position).magnitude <= ATTACK_RANGE + 1

			if inAttackRange then
				CanAttack = true
				if not attacking and CanAttack then
					print("Attacking")
					CanAttack = false
					attacking = true
					if Combo == 1 then
						Attack1Anim:Play()
						Combo = 2
						DoingCombo = 1
						
						delay(0.8,function()
							if Combo == 2 then
								Combo = 1
							end
						end)
						
					elseif Combo == 2 then
						Attack2Anim:Play()
						Combo = 3
						DoingCombo = 2
						
						delay(0.8,function()
							if Combo == 3 then
								Combo = 1
							end
						end)
						
					elseif Combo == 3 then
						Attack3Anim:Play()
						Combo = 4
						DoingCombo = 3
						
						delay(0.8,function()
							if Combo == 4 then
								Combo = 1
							end
						end)
						
					elseif Combo == 4 then	
						Attack4Anim:Play()
						Combo = 1
						DoingCombo = 4
						
					end	
					
					target.Parent.Humanoid.WalkSpeed = 1
					target.Parent.Humanoid.JumpPower = 0
					
					delay(0.72,function()
						if target ~= nil and target.Parent:FindFirstChild("Disabled") == nil then
							target.Parent.Humanoid.WalkSpeed = 16
							target.Parent.Humanoid.JumpPower = 50
						end	
					end)
					
					delay(0.12,function()
						if script.Parent:FindFirstChild("Disabled") == nil then
							if DoingCombo == 1 then
								target.Parent.Humanoid:TakeDamage(8)
							elseif DoingCombo == 2 then
								target.Parent.Humanoid:TakeDamage(8)
							elseif DoingCombo == 3 then
								target.Parent.Humanoid:TakeDamage(8)
							elseif DoingCombo == 4 then	
								target.Parent.Humanoid:TakeDamage(12)
								
								local Pos = script.Parent.HumanoidRootPart.CFrame*CFrame.new(0,0,-26)

								local BP = Instance.new("BodyPosition",target)
								BP.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
								BP.D = 90
								BP.P = 400
								BP.Position = Pos.p
								game.Debris:AddItem(BP,0.4)
							end	
							
							local Count = 0
							repeat
								Count = Count + 1
								HiteffectBall(target,target.CFrame * CFrame.new(math.random(-1,1),math.random(-1,1),math.random(-1,1)))
							until Count >= 5
							
							InsertDisabled(target.Parent,.55)
						end
					end)
					
					delay(0.6,function()
						attacking = false					
					end)
				end
			else
				runToTarget()
			end
		end	
	end

	-- Check if the current target no longer exists or is not attackable
	if target == nil then
		findTargets()
	end
end

local RunService = game:GetService("RunService")

RunService.Heartbeat:Connect(function()
	onHeartbeat()
end)

Hum.Died:Connect(function()
	script.Parent.Health:Destroy()
	script.Parent.Head.BillboardGui:Destroy()
	script.Parent.Healthandstuff:Destroy()
	
	for i,v in pairs(script.Parent:GetChildren()) do
		if v:IsA("Part") or v:IsA("MeshPart") then
			local TweenService = game:GetService("TweenService")
			
			local Tweeninf = TweenInfo.new(0.6)
			
			v.Anchored = true
			
			local DecayFX = FX.DecayFX:Clone()
			DecayFX.Parent = v
			DecayFX:Emit(70)
			
			local Tween = TweenService:Create(v,Tweeninf,{Transparency = 1})
			Tween:Play()
		end
	end
	
	script:Destroy()
end)

I have created an attacking npc but it keeps on attacking other npc’s how do i make it only attack actual players

Somewhere in there, only pick a target that is also a player

if Players:GetPlayerFromChar(target) then

I’m always amazed things like this don’t lag out using run service and looping through the entire workspace. Probably the quickest way to fix some of your problems would be to use collection service and tag members of the different teams differently. In my game, for example, I have a tag for “Red” (which happens to be the team name) and a tag for “NotRed” (etc). So,

CollectionService:GetTagged(“Not” … NPC:GetAttribute(“Team”))

This will fix your problem you had asked abou.t