I have a pathfinding script but the problem is I’m worried calling a lot of Path:ComputeAsync()s per second will lag a lot. I’m also trying to make an npc smoothly pathfind to a player with as least amount of lag. The problem is, it seems to call a lot of Path:ComputeAsync()s per second and it seems to start lagging a lot in terms of movement when you get close to the npc and move farther away. Here is the script:
local Player = game.Players.PlayerAdded:Wait() or game.Players:GetChildren()[1]
Player.CharacterAdded:Wait()
local PFS = game:GetService('PathfindingService')
local RunService = game:GetService('RunService')
local Path = PFS:CreatePath()
Path:ComputeAsync(script.Parent.Parent.HumanoidRootPart.Position,Player.Character.HumanoidRootPart.Position)
local PathFinished = true
while true do
if PathFinished and Path.Status == Enum.PathStatus.Success then
local Waypoints = Path:GetWaypoints()
local StartPos = Player.Character.HumanoidRootPart.Position
for i,v in pairs(Waypoints) do
coroutine.wrap(function()
PathFinished = false
Path:ComputeAsync(v.Position,Player.Character.HumanoidRootPart.Position)
PathFinished = true
end)()
script.Parent:MoveTo(v.Position)
script.Parent.MoveToFinished:Wait(2)
if i > 1 and (Player.Character.HumanoidRootPart.Position - StartPos).Magnitude > 1 then
print('Broke!')
break
end
end
else
print('aaaaaaaaaaaa')
if PathFinished then
Path:ComputeAsync(script.Parent.Parent.HumanoidRootPart.Position,Player.Character.HumanoidRootPart.Position)
end
RunService.Heartbeat:Wait()
end
end