Anyone know why the rig slows down over time when its wandering? After its been running for about 2 minutes it stops longer and longer in between each waypoint
I tried seeing if the issue was network ownership but it wasn’t
Anyone know what causes this issue?
function module:Wander()
wandering = true
pathfinding = false
following = false
if pathfindThread then
task.cancel(pathfindThread)
end
pathfindThread = task.spawn(function()
while not pathfinding and wandering do
local AllPaths = #path:GetChildren()
local randomPoint:Part = path:GetChildren()[math.random(1, AllPaths)]
repeat randomPoint = path:GetChildren()[math.random(1, AllPaths)] until randomPoint ~= lastPoint
lastPoint = randomPoint
humanoid:MoveTo(rig:GetPivot().Position)
local path = pathfindingService:CreatePath(agentParameters)
path:ComputeAsync(rig:WaitForChild("HumanoidRootPart").Position, randomPoint.Position)
local waypoints = path:GetWaypoints()
if path.Status == Enum.PathStatus.NoPath then
warn("Invalid pathfind: ".. rig.Name.. " move to "..tostring(randomPoint.Position).. " -Wander")
end
if showPathfind then
rig.Waypoints:ClearAllChildren()
for _, waypoint in ipairs(waypoints) do
local part = Instance.new("Part")
part.CanTouch = false
part.CanCollide = false
part.CanQuery = false
part.Anchored = true
part.Size = Vector3.one
part.Position = waypoint.Position
game.Debris:AddItem(part, 10)
part.Parent = rig.Waypoints
part.Name = "Path"
end
end
local totalWaypoints = #waypoints
local currentWaypoint = 0
for _, waypoint in ipairs(waypoints) do
if wandering and not following and not pathfinding then
humanoid:MoveTo(waypoint.Position)
currentWaypoint += 1
humanoid.MoveToFinished:Wait()
end
end
--print(currentWaypoint.. "/"..totalWaypoints)
if currentWaypoint == totalWaypoints and path.Status == Enum.PathStatus.Success then
print(rig.Name.." Reached path: "..tostring(randomPoint.Position).. " -Wander")
task.wait(4)
end
end
end)
end