NPCs attack each other and not the player

So, I am currently making npc’s that attack Players. And my npc’s attack themselves but not me. I do not know what the problem is. If anyone needs the script I will reply it to them. Thank you.

2 Likes

What’s the script? We need to see it to see what’s wrong.

1 Like

Chase or Zombie script? Wich one??

Zombie script. The one that does damage

Alright, here:

wait(1.5)
local sp = script.Parent
local Humanoid = sp:WaitForChild(“Humanoid”)
local Head = sp:WaitForChild(“Head”)
local UpperTorso = sp:WaitForChild(“UpperTorso”)
local Animation = script:WaitForChild(“AttackAnim”)
local Hit1 = Instance.new(“Sound”,UpperTorso)
Hit1.Volume = 1
Hit1.SoundId = " "
Hit1.Pitch = 0.15
local Hit2 = Instance.new(“Sound”,UpperTorso)
Hit2.Volume = 1
Hit2.SoundId = " "
Hit2.Pitch = 0.15

local AttackEnabled = true

function wait(TimeToWait)
if TimeToWait ~= nil then
local TotalTime = 0
TotalTime = TotalTime + game:GetService(“RunService”).Heartbeat:wait()
while TotalTime < TimeToWait do
TotalTime = TotalTime + game:GetService(“RunService”).Heartbeat:wait()
end
else
game:GetService(“RunService”).Heartbeat:wait()
end
end

function DamageTag(parent,damage)
local DmgTag = script.DamageTag:clone()
DmgTag.Damage.Value = damage
DmgTag.creator.Value = game.Players.LocalPlayer
DmgTag.Disabled = false
DmgTag.Parent = parent
end

local Anim = Humanoid:LoadAnimation(Animation)
function Hit(hit)
local TargetHum = hit.Parent:FindFirstChild(“Humanoid”)
script.Parent:WaitForChild(“Sword”).Trail.Enabled = true
if TargetHum ~= nil and TargetHum:IsA(“Humanoid”) and AttackEnabled == true and TargetHum ~= Humanoid then
AttackEnabled = false
Delay(0.5,function() AttackEnabled = true end)
if Anim then Anim:Play(nil,nil,1.5) end
script.Parent:WaitForChild(“Sword”).Trail.Enabled = false
DamageTag(TargetHum.Parent,5)
Hit1:Play()
Hit2:Play()
end
end

for _, Child in pairs(script.Parent:GetChildren()) do
if Child:IsA(“Part”) or Child:IsA(“WedgePart”) or Child:IsA(“CornerWedgePart”) then
Child.Touched:connect(Hit)
end
end

Maybe change the npc’s humanoid’s name to something other than “Humanoid” because the parts of the humanoid are hitting each other, and the function does damage if the character has a child named “Humanoid”

I tried. It completely stops moving and attacking. Doesn’t even attack me.

Was there an error in the output when you changed the name away from humanoid?

Mhm. “Humanoid is not a valid member of Model”

An easier way to detect if you are hitting a player is if Players:GetPlayerFromCharacter(hit.Parent) returns a Player Instance (and not nil), then you know you’re 100% hitting a player and not an NPC. You can read more on this here.

3 Likes

They keep on trying to attack each other. But now they cannot hit themselves.

Do you use PathFinding Service to detect where the nearest Player Character is? If not, I highly recommend reading up on it; if implemented properly, it will only move to the nearest Player and reroute when the player moves, which is what you want.

2 Likes

just group them if you don’t want them to attack each other

This isn’t necessary as I could easily whitelist any other character that has the same name as the zombie. Anyway, please don’t bump this post anymore, it’s over a year old and I’ve found a simple solution.