Help with NPC following another NPC script

So I’ve already a script for it to attack other NPC’s but I can’t seem to get an NPC to follow another NPC if it gets close enough.
This is my current script for it. And it doesn’t print any errors.

 local npc = script.Parent
 local hrpOfNPC = npc:WaitForChild("HumanoidRootPart")
 local maxDistance = 100

while wait(0) do

local plrs = game.Workspace:GetChildren()

local closestHRP

for i, enemy in pairs(plrs) do
	if enemy:FindFirstChild("Humanoid") and enemy:FindFirstChild("Humanoid").Health > 0 and not game.Players:FindFirstChild(enemy.Name) then
		
		local hrp = enemy:FindFirstChild("HumanoidRootPart")

		local distanceBetween = (hrpOfNPC.Position - hrp.Position).Magnitude


		if not closestHRP then closestHRP = hrp end

		if (hrpOfNPC.Position - closestHRP.Position).Magnitude > distanceBetween then

			closestHRP = hrp

		end
	end
	end


if closestHRP and (hrpOfNPC.Position - closestHRP.Position).Magnitude <= maxDistance then npc.Humanoid:MoveTo(closestHRP.Position) end
end
2 Likes

Ouch.

Rule of thumb is to avoid wait() and wait(X) where X is a small number.

Recommendation:

while RunService.Stepped:Wait() do 

Also store the distance in a variable because Magnitude uses sqrts which are quite costly when used a lot.

Also for these parts use Collection Service. Alternatively, put the NPCs into a Folder you can easily reference.

Then you will not need to have as many if statements

You might want to simplify this. Your logic is almost correct but I think you

Rewritten:

for _, NPC in ipairs (CollectionService:GetTagged("NPC")) do -- Assuming tag is called NPC
    if NPC.Humanoid.Health < 0 then
        continue
    end
    local NPCVector = NPC.HumanoidRootPart - HumanoidRootPart.Position 
    local NPCDistance = NPCVector.Magnitude
    if not ClosestNPC or NPCDistance < ClosestNPCDistance then
        ClosestNPC = NPC
        ClosestNPCDistance = NPCDistance
        Humanoid:Move(NPCVector)
    end
end


You could also try printing out the values of the loop of it doesn’t work.

Okay, so I’m not really sure what I’m doing, but after following some of your steps now I have somehow got it to move towards the enemy NPC’s direction when within reach, but the problem is that it doesn’t stop once it gets there, it instead continues to move in that direction passing by the enemy. Would you have any idea how to fix that?
Here’s the current script:

local npc = script.Parent
local RunService = game:GetService("RunService")
local CollectionService = game:GetService("CollectionService")

while RunService.Stepped:Wait() do 


local enemies = game.Workspace.Enemies:GetChildren()

local ClosestHRP

for i, enemy in pairs(enemies) do
if enemy:FindFirstChild("Humanoid") and enemy:FindFirstChild("Humanoid").Health > 0 and not 
game.Players:FindFirstChild(enemy.Name) then
	
	local HumanoidRootPart = enemy:FindFirstChild("HumanoidRootPart") -- of enemy
    local Humanoid = npc:FindFirstChild("Humanoid") -- of npc
	local distanceBetween = (hrpOfNPC.Position - HumanoidRootPart.Position).Magnitude
	
		if enemy.Humanoid.Health < 0 then
			continue
		end
	local NPCVector = enemy.HumanoidRootPart.Position - npc.HumanoidRootPart.Position 
		local NPCDistance = NPCVector.Magnitude
	if not ClosestNPC or NPCDistance < ClosestNPCDistance then
		if NPCDistance < maxDistance then
			ClosestNPC = enemy
			ClosestNPCDistance = NPCDistance
			Humanoid:Move(NPCVector)
	 	end
			
		
		end
	end

end

end

You could switch this to MoveTo instead.

Sorry about that I didn’t fully understand what you wanted it to do.

MoveTo is a way to have the NPC travel to a specific point.

Move is a way to have the NPC travel in a specified direction.

Move does not stop when the NPC gets to a certain position however what it can do is allow you to have more control over the speed of the NPC movement and you can make the NPC Travel in a curve unlike MoveTo.

In other words if you want it to just stop you can use MoveTo instead.

Sorry for the misunderstanding! :smiley: