NPC following other NPC's and killing them

Hello Developers! I need some help with my NPC monsters. So I have made an NPC that is supposed to follow a player then hit them and kill them.
The way I made it follow players is that I used this script Inside of the Monster:

local larm = script.Parent:FindFirstChild("HumanoidRootPart")
local rarm = script.Parent:FindFirstChild("HumanoidRootPart")

function findNearestTorso(pos)
 local list = game.Workspace:children()
 local torso = nil
 local dist = 1000000000000
 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 - pos).magnitude < dist then
     torso = temp
     dist = (temp.Position - pos).magnitude
    end
   end
  end
 end
 return torso
end




while true do
 wait(math.random(1,5))
 local target = findNearestTorso(script.Parent.HumanoidRootPart.Position)
 if target ~= nil then
  script.Parent.Humanoid:MoveTo(target.Position, target)
 end

end

Then I put a kill on touch script in all the body parts like the torso and the lower arm etc.

This is the kill on touch script:

function onTouched(hit)
	if not hit or not hit.Parent then return end
	local human = hit.Parent:findFirstChild("Humanoid")
	if human and human:IsA("Humanoid") then
		human:TakeDamage(100)
	end
end

script.Parent.Touched:connect(onTouched)

But the NPC just follows other NPCs and then kills them. How can I modify these scripts to only follow players and only do damage to players?

4 Likes

Can anyone help me please?

30 characters
r8748392437283

All NPC’s have a Humanoid
Try checking for a Backpack instead of Humanoid

Only players have Backpack

Or make the NPC’s Humanoid Type to R6 and make the game Rthro or R15 and search for HumanoidRootPart.
R6 have Torso instead of HumanoidRootPart

I have never checked this code but give it a try.

1 Like

Oh unfortunately I want it to be R15 also searching for a backpack seems a little complicated. If there is another way I will use it if not i will use yours

1 Like

You never checked if the model actually belonged to a player. Use the GetPlayerFromCharacter function on a model to check. Players | Documentation - Roblox Creator Hub

Like this: if (temp2.className == "Model") and (temp2 ~= script.Parent) and game.Players:GetPlayerFromCharacter(temp2) then

1 Like

Oh thankyou so much. Also how do I do that in my damage script?

Ok then try make all NPC’s name to something similar like Zombie

And type

if not Target.Name == "Zombie" then -- checking if the name is not Zombie and also make a parameter called Target
   -- Code
end

You see the code I sent may not be correct you need to check if the name is not zombie.

Hope you understand

1 Like

Its fine I can use the hboogy101s way.

1 Like

Thanks to hboogy101 it works! Thankyou so much!

Also don’t forget to make @hboogy101’s post the solution if it is correct. :smiley:

1 Like

I have

30characterssssssssssssssssssssss

function onTouched(hit)
	if not hit or not hit.Parent or not game.Players:GetPlayerFromCharacter(hit.Parent) then return end
	local human = hit.Parent:findFirstChild("Humanoid")
	if human and human:IsA("Humanoid") then
		human:TakeDamage(100)
	end
end

script.Parent.Touched:connect(onTouched)
1 Like

Nice work bro :ok_hand:
You are very good at this

1 Like

Yes thankyou so much! You have made my day!:tada:

1 Like