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?
-- Get Pathfinding Service
local pathfindingService = game:GetService("PathfindingService")
-- Variales For NPC Humanoid Torso And Destination
local humanoid = script.Parent.Humanoid
local body = script.Parent:FindFirstChild("HumanoidRootPart") or script.Parent:FindFirstChild("Torso")
local destination = script.Parent.Parent.Target.Position
-- Create Path Object
local path = pathfindingService:CreatePath()
-- Compute A Path
path:ComputeAsync(body.Position, destination)
-- Get The Waypoints Table
local waypoints = path:GetWaypoints()
-- Main
for k, waypoint in pairs(waypoints) do
humanoid:MoveTo(waypoint.Position)
if waypoint.Action == Enum.PathWaypointAction.Jump then
humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
end
humanoid.MoveToFinished:Wait()
end
script.Parent.Parent:Destroy()
And also I made some changes looking to other forums..
-- Get Pathfinding Service
local pathfindingService = game:GetService("PathfindingService")
-- Variales For NPC Humanoid Torso And Destination
local humanoid = script.Parent.Humanoid
local body = script.Parent:FindFirstChild("HumanoidRootPart") or script.Parent:FindFirstChild("Torso")
local destination = script.Parent.Parent.Target.Position
-- Create Path Object
local path = pathfindingService:CreatePath()
-- Compute A Path
path:ComputeAsync(body.Position, destination)
-- Get The Waypoints Table
local waypoints = path:GetWaypoints()
-- Main
local distance
for k, waypoint in pairs(waypoints) do
humanoid:MoveTo(waypoint.Position)
if waypoint.Action == Enum.PathWaypointAction.Jump then
humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
end
repeat
distance = (waypoint.Position - humanoid.Parent.PrimaryPart.Position).magnitude
wait(0.0001)
until
distance <= 50
end
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.
