So I am in need of some help with the new pathfinding system. I am currently trying to make an npc that follows the player with pathfinding and do not know how to update its path to recalculate the path if the player moves without the npc just freezing while the player is moving. Currently this is my code
local zombie = script.Parent local human = zombie:FindFirstChild("Humanoid") local zombieRoot = zombie:FindFirstChild("HumanoidRootPart") local pathFindingService = game:GetService("PathfindingService") wait(1) if not human then return end while wait() do if script.Parent.Agro.Value == false then local players = {} for i,v in pairs(game.Players:GetChildren()) do local char = v.Character if char then local root = v.Character:FindFirstChild("HumanoidRootPart") if (root.Position - zombieRoot.Position).magnitude <= 25 then table.insert(players, v.Character) end end end if players[1] ~= nil then local locations = {} for i,v in pairs(players) do local pos = (v.HumanoidRootPart.Position - zombieRoot.Position).magnitude table.insert(locations, pos) end local key, max = 1, locations[1] for i,v in pairs(locations) do if locations[i] > max then key, max = i, v end end local plr = players[key] script.Parent.Agro.Value = true script.Parent.Target.Value = plr repeat wait() local start = script.Parent.HumanoidRootPart local finish = script.Parent.Target.Value.HumanoidRootPart.Position local path = pathFindingService:FindPathAsync(start.Position, finish) local waypoints = path:GetWaypoints() for i,v in ipairs(waypoints) do if finish ~= script.Parent.Target.Value.HumanoidRootPart.Position then break else human:MoveTo(v.Position) human.MoveToFinished:Wait() if v.Action == Enum.PathWaypointAction.Jump then human.Jump = true end end end until plr.Humanoid.Health < 1 or (plr.HumanoidRootPart.Position - zombieRoot.Position).magnitude > 30 script.Parent.Agro.Value = false script.Parent.Target.Value = nil end end end
If anyone could help and shine some light on this that would be awesome, thank you.