[SOLVED] How to fix stuttering Npc lag in pathfinding?
Asked by
4 years ago Edited 4 years ago
I see a lot of pathfinding lag and NPC stuttering while running the game. Although, it doesn't happen when I run it. So, I think its a Roblox created lag. So, how can I fix it?
02 | local pathfindingService = game:GetService( "PathfindingService" ) |
05 | local humanoid = script.Parent.Humanoid |
06 | local body = script.Parent:FindFirstChild( "HumanoidRootPart" ) or script.Parent:FindFirstChild( "Torso" ) |
07 | local destination = script.Parent.Parent.Target.Position |
10 | local path = pathfindingService:CreatePath() |
13 | path:ComputeAsync(body.Position, destination) |
16 | local waypoints = path:GetWaypoints() |
19 | for k, waypoint in pairs (waypoints) do |
20 | humanoid:MoveTo(waypoint.Position) |
22 | if waypoint.Action = = Enum.PathWaypointAction.Jump then |
23 | humanoid:ChangeState(Enum.HumanoidStateType.Jumping) |
26 | humanoid.MoveToFinished:Wait() |
29 | script.Parent.Parent:Destroy() |
And also I made some changes looking to other forums..
02 | local pathfindingService = game:GetService( "PathfindingService" ) |
05 | local humanoid = script.Parent.Humanoid |
06 | local body = script.Parent:FindFirstChild( "HumanoidRootPart" ) or script.Parent:FindFirstChild( "Torso" ) |
07 | local destination = script.Parent.Parent.Target.Position |
10 | local path = pathfindingService:CreatePath() |
13 | path:ComputeAsync(body.Position, destination) |
16 | local waypoints = path:GetWaypoints() |
20 | for k, waypoint in pairs (waypoints) do |
21 | humanoid:MoveTo(waypoint.Position) |
23 | if waypoint.Action = = Enum.PathWaypointAction.Jump then |
24 | humanoid:ChangeState(Enum.HumanoidStateType.Jumping) |
28 | distance = (waypoint.Position - humanoid.Parent.PrimaryPart.Position).magnitude |
34 | script.Parent.Parent:Destroy() |
But the problem in second script is that the NPC move one step and waits for 2-3 sec and move another step.
Please help.