wait(2) function FindNearest(position) local lowest = math.huge -- infinity local NearestPlayer = nil for i,v in pairs(game.Players:GetPlayers()) do if v and v.Character then local distance = v:DistanceFromCharacter(position) if distance < lowest then lowest = distance NearestPlayer = v end end end return NearestPlayer end local PathfindingService = game:GetService("PathfindingService") -- Variables for the zombie, its humanoid, and destination local zombie = script.Parent local humanoid = zombie.Humanoid local destination = FindNearest(zombie.Torso.Position).Character print(FindNearest(zombie.Torso.Position)) -- Create the path object local path = PathfindingService:CreatePath() -- Variables to store waypoints table and zombie's current waypoint local waypoints local currentWaypointIndex local function followPath(destinationObject) -- Compute and check the path path:ComputeAsync(zombie.HumanoidRootPart.Position, destinationObject.PrimaryPart.Position) -- Empty waypoints table after each new path computation waypoints = {} if path.Status == Enum.PathStatus.Success then -- Get the path waypoints and start zombie walking waypoints = path:GetWaypoints() -- Move to first waypoint currentWaypointIndex = 1 humanoid:MoveTo(waypoints[currentWaypointIndex].Position) else -- Error (path not found); stop humanoid humanoid:MoveTo(zombie.HumanoidRootPart.Position) end end local function onWaypointReached(reached) if reached and currentWaypointIndex < #waypoints then currentWaypointIndex = currentWaypointIndex + 1 humanoid:MoveTo(waypoints[currentWaypointIndex].Position) end end local function onPathBlocked(blockedWaypointIndex) -- Check if the obstacle is further down the path if blockedWaypointIndex > currentWaypointIndex then -- Call function to re-compute the path followPath(destination) end end -- Connect 'Blocked' event to the 'onPathBlocked' function path.Blocked:Connect(onPathBlocked) -- Connect 'MoveToFinished' event to the 'onWaypointReached' function humanoid.MoveToFinished:Connect(onWaypointReached) followPath(destination)
Here is some code I stitched together for a zombie NPC I made. It works except once it makes it to the original position of the target it stops and doesn't move. I want it to keep finding a new path and continuing to follow the player and their new position. I tried placing a while loop at the end that reset the destination and re-found the path (shown below).
while wait(0.5) do local destination = FindNearest(zombie.Torso.Position).Character followPath(destination) end
This worked for the most part except the movement was extremely jerky. Any suggestions would be great.
Hello! I think I may have the solution humanoid.MoveToFinished:Connect(onWaypointReached)
does not fire any functions that create another path.
So for your lines of script:
local function onWaypointReached(reached) if reached and currentWaypointIndex < #waypoints then currentWaypointIndex = currentWaypointIndex + 1 humanoid:MoveTo(waypoints[currentWaypointIndex].Position) end end
Make it so it will repeatly create another path and follow it. I don't understand pathfindingservice so beware of my answer!