Answered by
2 years ago Edited 2 years ago
This is because even though you are looping through all players, only the path away from first player is calculated. Second and subsequent players will not be taken into account, until the humanoid reaches the end of the calculated path.
In practice it will never happen though, as You have connected Your function to the Heartbeat. So by the time the loop reaches second player, another Heartbeat recalculates everything. Heartbeat runs up to 60FPS!
So this script is essentially reverse zombie. As such, NPS must find the NEAREST player, and try to run away from it. To avoid grinding server resources to the ground, I suggest to re-check which player is closest about each second. Then you can adjust it to needs as needed.
Connecting to Heartbeat is an Overkill imho.
EDIT 2:
It seems your pathfinding targets are wrong, and I do not know how did it work for you previously. For NPC to run away along the path, you have to choose a specific point on the map during the COMPUTE phase, rather than adjusting the path afterwards.
01 | local currentFollowedRoot |
03 | local function RunAway(RootPart) |
05 | local NewPath = PathFindingService:CreatePath() |
06 | NewPath:ComputeAsync(NPC.HumanoidRootPart.Position, NPC.HumanoidRootPart.Position * Vector 3. new( 10 , 0 , 0 )) |
08 | if NewPath.Status = = Enum.PathStatus.Success then |
10 | local Waypoints = NewPath:GetWaypoints() |
11 | for _, Waypoint in pairs (Waypoints) do |
13 | if Waypoint.Action = = Enum.PathWaypointAction.Jump then |
17 | Humanoid:MoveTo(Waypoint.Position) |
18 | Humanoid.MoveToFinished:Wait() |
20 | if currentFollowedRoot ~ = RootPart then |
38 | for _, plr in pairs (Players:GetPlayers()) do |
41 | local root = plr.Character:WaitForChild( 'HumanoidRootPart' ) |
42 | local magnitude = (root.Position - |
43 | NPC.HumanoidRootPart.Position).Magnitude |
44 | if magnitude < Radius then |
52 | currentFollowedRoot = nearestRoot |
EDIT: Added coroutine for the RunAway function, to prevent it from pausing the loop.
EDIT 2: Fixed the pathfind.