Enemy NPC Pathfinding not working

Hello, I’m trying to get this enemy to path find their way towards you if you get close enough but it doesn’t seem to move. How do I solve this?

-- 
local hum = script.Parent:WaitForChild("Humanoid")
local root = script.Parent:WaitForChild("HumanoidRootPart")
local spawnPos = script.Parent:WaitForChild("SpawnPosition")

local visionRange = 30
local attackRange = 3
local abandonRange = 40
local abandonRangeFromHome = 200

local target = nil

while wait() do
	
	if target then
		
		local plrRoot = target.HumanoidRootPart
		local distance = (root.Position - plrRoot.Position).magnitude
		local distancefromhome = (spawnPos.Value - plrRoot.Position).magnitude
		
		local PathfindingService = game:GetService("PathfindingService")

		local calculation = hum.WalkSpeed/3.675
		local TimePerStep = 1/calculation

		local HumanoidProperties = {
			["AgentHeight"] = 5,
			["AgentRadius"] = visionRange,
			["AgentCanJump"] = true
		}
		--Makes Path
		local Path = PathfindingService:CreatePath(HumanoidProperties)
		--Makes Humanoid Follow Waypoints to a target
		function GoTo(Target)
			--Computes Path and Waypoints
			Path:ComputeAsync(root.Position, Target.Position)
			if Path.Status == Enum.PathStatus.NoPath then
			else
				local WayPoints = Path:GetWaypoints()
				--Makes humanoid follow the Waypoints
				for _, WayPoint in ipairs(WayPoints) do

					hum:MoveTo(plrRoot.Position - CFrame.new(root.Position, plrRoot.Position).LookVector * attackRange)

					wait(TimePerStep)
					if WayPoint.Action == Enum.PathWaypointAction.Jump then
						hum.Jump = true
					end
				end
			end
		end
		
		while wait(0.25) do
			GoTo(target)
		end
		
		if distance <= attackRange + 2 then
			script.AttackRemote:Fire(plrRoot)
		end
		
		if distance > abandonRange then
			target = nil
			hum:MoveTo(spawnPos.Value)
		end
		
		if distancefromhome > abandonRangeFromHome then
			target = nil
			hum:MoveTo(spawnPos.Value)
		end
	else
		for i,v in pairs(game.Players:GetChildren()) do
			if not game.Workspace:FindFirstChild(v.Name) then continue end
			local char = game.Workspace[v.Name]
			local plrRoot = char.HumanoidRootPart
			
			local distance = (root.Position - plrRoot.Position).magnitude
			local distancefromhome = (spawnPos.Value - plrRoot.Position).magnitude
			
			if distance < visionRange and distancefromhome < abandonRangeFromHome then
				target = char
			end
		end
	end
end

What do you mean by “not working”?

Is it not moving at all? Is it stuttering when it gets close? Could you show a video?

Can you try changing the local plrRoot = target.HumanoidRootPart to local plrRoot = target:FindFirstChild(“HumanoidRootPart”) and make sure to detect if it exists?

The enemy is not moving at all.

Please explain what part of it isn’t working when you create a post like this.

  • Is the pathing system not creating a path?
  • Is the player detection not working?
  • Is the movement of the NPC not working?

Often times you’ll find the answer yourself by actually investigating the failure point.

Looking briefly through your script - something that strikes me as odd is your HumanoidProperties - your ‘Agent Radius’ is set to the ‘visionRange’ - which is 30.

However, AgentRadius defines the space your NPC should be able to fit through, rather than the viewing range.
So in essence you’re asking your pathfinding to find a space that is 30 studs wide in order to path to your target.

Set this to a lower value like 1 or 2.

-Edit-
Another oddity is that you seem to have an infinite loop with your GoTo function.
You either need some sort of exit for this, or to remove the loop entirely in order for the rest of your script to continue.

Going forward, please use Prints and Breakpoints to help diagnose your scripts, they’ll aid you in finding infinite loops.

1 Like

Is HumanoidRootPart is anchored?

If not, try adding this:

if Path.Status ~= Enum.PathStatus.Success then
	warn("PATH CANNOT BE CALCULATED")
else

I changed line 40 to:
Path:ComputeAsync(root.Position, Target.HumanoidRootPart.Position).

It now can follow me but it still can be blocked by parts. Also, the warning appeared.