PathFinding NPC dosen't move and takes time for them to change waypoint

The Zombies went to waypoint but it takes time for them to go to the new waypoint and sometimes they would stop moving and stuck there.

I have tried to use change the distance(until distance < 7) for them to change waypoint but it still doesn’t work.
I am not sure what is the problem, but maybe it is because there are too many waypoints to go so they don’t know where to go

If you could help me, I would be really happy, thank you very much!

local root = script.Parent:WaitForChild("HumanoidRootPart")
	local rand = Random.new()
 

	    
	    
        
	       
	       
            


function Path()
    local players = game.Players:GetPlayers()
    local NearestZombie = players[1]
if #players > 0 then
	local pRoot = NearestZombie.Character:WaitForChild("HumanoidRootPart")
  if NearestZombie.Character and NearestZombie.Character.Humanoid.Health > 0 then
	

    for i = 2, #players do
        local Distance1 = (NearestZombie.HumanoidRootPart.Position - pRoot.Position).Magnitude
        local Distance2 = (players[i].HumanoidRootPart.Position - pRoot.Position).Magnitude

        if Distance2 < Distance1 then
            NearestZombie = players[i]
        end
    end
end

	            local path = game:GetService("PathfindingService"):CreatePath()
	path:ComputeAsync(root.Position, pRoot.Position, 100)
	            local points = path:GetWaypoints()
	 
	          for _, waypoint in pairs(points) do
                script.Parent.Zombie:MoveTo(waypoint.Position)
	 
                repeat -- Wait until we are close to a point before moving to the next
                    local distance = (waypoint.Position - root.Position).magnitude
	                    wait()
	                until distance < 10
	            end
	        end
	    end
	
	 
	-- The number inside wait will determine how long the zombie idles at
	-- the end of a path before starting to follow another player
	while wait() do
	    Path()
	
	end


1 Like

Sorry i keep changing the script

It looks like because your distance variable is local, and you are checking its value after until, the loop will never break, which means the zombie will just wait forever.

Try declaring the distance variable above the repeat loop, and update it on repeat instead.

Nothing else really stands out to me other than that. It’s kind of late.

I am new to this platform so i don’t really understand how does this effect the script.
Sorry!

1 Like

So if you declare a local variable within the loop, it only exists within that loop, and nowhere else. You declare the variable inside the loop, but check its value outside the loop after the until.

Since the variable doesn’t exist outside the loop, there is no value when checked outside the loop, which means the loop will never break on condition.

So it if i putted the Variable outside of the loop and update it.
Is it like this

for _, waypoint in pairs(points) do
               script.Parent.Zombie:MoveTo(waypoint.Position)
    local distance = (waypoint.Position - root.Position).magnitude
               repeat -- Wait until we are close to a point before moving to the next
                   script.Parent.Zombie:MoveTo(waypoint.Position)
                       wait()
                   until distance < 10```

Like this:

for _, waypoint in pairs(points) do
    script.Parent.Zombie:MoveTo(waypoint.Position)
    local distance
    repeat -- Wait until we are close to a point before moving to the next
          distance = (waypoint.Position - root.Position).magnitude
          script.Parent.Zombie:MoveTo(waypoint.Position)
          wait()
    until distance < 10

Declare outside, update inside.

2 Likes

Thank You For Helping Me , I appreciate your kindness

Oh one more thing, how do i make the NPC to not go all the way to the old waypoint then go to my current waypoint

Oh one more thing, how do I make the NPC to not go all the way back to the old waypoint then go to my current waypoint

The npc need to go to the original waypoint before going to the new waypoint.


local root = script.Parent:WaitForChild("HumanoidRootPart")
   local rand = Random.new()


      

function Path()
   local players = game.Players:GetPlayers()
   local NearestZombie = players[1]

   local pRoot = NearestZombie.Character:WaitForChild("HumanoidRootPart")
 if NearestZombie.Character and NearestZombie.Character.Humanoid.Health > 0 then
   

   for i = 2, #players do
       local Distance1 = (NearestZombie.HumanoidRootPart.Position - pRoot.Position).Magnitude
       local Distance2 = (players[i].HumanoidRootPart.Position - pRoot.Position).Magnitude

       if Distance2 < Distance1 then
           NearestZombie = players[i]
       end
   end
end

               local path = game:GetService("PathfindingService"):CreatePath({AgentRadius=6,AgentHeight=6})
   path:ComputeAsync(root.Position, pRoot.Position)
               local points = path:GetWaypoints()
    
    for _, waypoint in pairs(points) do

   script.Parent:WaitForChild("Zombie"):MoveTo(waypoint.Position)
   local distance
   repeat -- Wait until we are close to a point before moving to the next
         distance = (waypoint.Position - root.Position).magnitude

         script.Parent:WaitForChild("Zombie"):MoveTo(waypoint.Position)
         wait()
   until distance < 20

end
end

    
   -- The number inside wait will determine how long the zombie idles at
   -- the end of a path before starting to follow another player
   while wait() do
       Path()
   
   end
2 Likes