I have used a tutorial pathfinding script as a base, then built on top of it for the functionality I need. An issue I found very early is that after chasing the player for long enough (after creating ~150 ComputeAsync paths) the ComputeAsync becomes very obviously laggy and stutters when trying to create a path., eventually creating only one waypoint over dozens of seconds. I have narrowed this down to the ComputeAsync function as the rest of the script runs fine and there’s nothing else in the game to create a memory leak elsewhere. It is very frustrating because the pathfinding works perfect at first, then seemingly deteriorates after running for a while.
None of the solutions I have come up with or looked up online have worked. I have tried refreshing every variable every path creation, I have tried refreshing every variable on a timer, I have tried giving network ownership of the NPC to the player, I have tried copy and pasting a completely different person’s pathfinding script (that also used ComputeAsync), disabling and reenabling the script also did nothing, and, my absolute last resort, deleting the pathfinding script and duplicating an exact copy back into the NPC also did not help.
I will clarify just to be absolutely safe. I do not need someone to create a pathfinding script for me; that is already done. The pathfinding is not “failing” (blocked by obstacles, inaccessible platform, etc.), it is just taking forever to create the waypoints. I have already tried flushing the variables to be brand new in multiple ways, if this is the issue then it must be some hidden method that somehow eluded me this entire time.
function Pathfind()
while SearchAttempts <= 10 do
local waypoints = nil
target = nil
path = nil
task.wait(0.25)
path = PathfindingService:CreatePath({
AgentCanJump = false,
AgentCanClimb = false,
Costs = {
Neon = 1000,
Plastic = 1,
DangerZone = 10000,
},
AgentRadius = 2.7,
AgentHeight = 2,
})
GetClosestPlayer()
if GetClosestPlayer() ~= nil then
npc:SetAttribute("Target",GetClosestPlayer().Name)
npc:SetAttribute("HasTarget",true)
local GoalPlayer = GetClosestPlayer()
if GoalPlayer ~= nil then
target = game.Workspace.Players[npc:GetAttribute("Target")].HumanoidRootPart.Position
else
end
local succ, err = pcall(function()
path:ComputeAsync(npc.PrimaryPart.Position,target)
end)
if npc:GetAttribute("SeesTarget") == true and succ and path.Status == Enum.PathStatus.Success then
npc:SetAttribute("State","Chasing")
npc:SetAttribute("HasTarget",true)
--print("NPC has a target.")
else
--warn(err)
end
if npc:GetAttribute("State") == "Chasing" then
if npc.FollowPlayer.Enabled == false then
npc.FollowPlayer.Enabled = true
end
elseif npc:GetAttribute("State") == "Searching" then
if npc.FollowPlayer.Enabled == true then
npc.FollowPlayer.Enabled = false
end
for i, waypoint in pairs(path:GetWaypoints()) do
local part = Instance.new("Part")
part.Material = "Neon"
part.Anchored = true
part.CanCollide = false
part.Shape = "Ball"
part.Position = waypoint.Position
part.Parent = game.Workspace.whoa
hum:MoveTo(waypoint.Position)
game.Debris:AddItem(part,1)
hum.MoveToFinished:Wait()
end
path:Destroy()
--print("End of path.")
if npc:GetAttribute("SeesTarget") == false then
if npc.FollowPlayer.Enabled == true then
npc.FollowPlayer.Enabled = false
end
npc:SetAttribute("State","Node")
end
if npc:GetAttribute("State") == "Node" then
target = OGSpot
local succ, err = pcall(function()
path:ComputeAsync(npc.PrimaryPart.Position,target)
end)
for _, waypoint in pairs(path:GetWaypoints()) do
local part = Instance.new("Part")
part.Material = "Neon"
part.Anchored = true
part.CanCollide = false
part.Shape = "Ball"
part.Color = Color3.new(1, 0, 1)
part.Position = waypoint.Position
part.Parent = game.Workspace.whoa
game.Debris:AddItem(part,1)
hum:MoveTo(waypoint.Position)
hum.MoveToFinished:Wait()
end
path:Destroy()
npc:SetAttribute("State","Idle")
end
end
else
npc:SetAttribute("HasTarget",false)
npc:SetAttribute("Target",nil)
end
--SearchAttempts += 1
end
end
I am putting my script here incase I am doing something horribly wrong, but I imagine you could replicate my issue by placing 2 nodes and having an NPC path between them for long enough (this was another test I did but with 3 nodes instead of 2, it still eventually broke). It’s not the entire script, but the other functions work as intended regardless of the ComputeAsync breaking or not.
