local PathfindingService = game:GetService("PathfindingService")
local ComputationQueue = {}
local ActiveRequest = 0
local Battles = workspace.Battles:GetChildren()
workspace.Battles.ChildAdded:Connect(function(Child)
table.insert(Battles,Child)
end)
workspace.Battles.ChildRemoved:Connect(function(Child)
local Index = table.find(Battles,Child)
if Index then
table.remove(Battles,Index)
end
end)
task.spawn(function()
while true do
task.wait(#Battles * 0.1)
if #Battles == 0 then continue end
local EarliestID
local EarliestInfo
local PreviousTime
for ID,Info in pairs(ComputationQueue) do
if not (Info.Request and Info.QueueTime) then continue end
task.wait()
if not PreviousTime then
EarliestID = ID
EarliestInfo = Info
PreviousTime = Info.QueueTime
elseif (tick() - PreviousTime) < (tick() - Info.QueueTime) then
EarliestID = ID
EarliestInfo = Info
PreviousTime = Info.QueueTime
end
end
if EarliestID and ComputationQueue[EarliestID] then
local Index = ComputationQueue[EarliestID] or {}
local Path = PathfindingService:CreatePath(Index.Params or {})
Path:ComputeAsync(Index.StartPos,Index.GoalPos)
print(Path.Parent)
if Path.Status == Enum.PathStatus.Success then
ComputationQueue[EarliestID] = {Path=Path}
else
local Attempts = Index.Attempts or 0
Attempts += 1
if Attempts > 2 then
ComputationQueue[EarliestID].Request = nil
else
ComputationQueue[EarliestID].Attempts = Attempts
end
end
end
end
end)
local module = {}
module.ActiveRequest = ActiveRequest
module.ComputationQueue = ComputationQueue
module.RequestComputation = function(ID,StartPos,GoalPos,Params)
if ComputationQueue[ID] then return end
ActiveRequest += 1
ComputationQueue[ID] = {Request=true,StartPos=StartPos,GoalPos=GoalPos,QueueTime=tick()}
repeat task.wait() until not (ComputationQueue[ID] and ComputationQueue[ID].Request)
ActiveRequest -= 1
local Path = ComputationQueue[ID] and ComputationQueue[ID].Path
ComputationQueue[ID] = nil
return Path
end
return module
This code has a memory leak in it and I’m unsure of where it lies. The script I call this code with is AIHandler as you can see it’s inactive with 3k rate Screenshot by Lightshot