The enemy AI for a game i’m trying to make is having an unusually high rate along with memory leakages
To summarize, this is the rate.
Rate is essentially the amount of times scripts run per second, and there usually is about 20 - 50
of these scripts on average so i don’t know why the rate is 60k
and activity is also pretty high too, there would also be intense lag along with memory leaks
-- Debounces
local Blocked = false
local Pathing = false
-- Essentials
local TargetToMoveTo
local MoveTo
local Waypoint
local print = DebugMode and print or function() end
local CurrentIndex
local PathParams = {AgentHeight = Character:GetModelSize().Y, AgentRadius = Character:GetModelSize().X, AgentCanJump = CanJump, AgentCanClimb = CanClimb, WaypointSpacing = Spacing, Costs = CostConfigs}
local Path:Path = PathfindingService:CreatePath{table.unpack(PathParams)}
local BlockConn
task.spawn(function() -- Get Target
while task.wait(.2) and Humanoid:GetState() ~= Enum.HumanoidStateType.Dead do
local Check = GetNearestTarget(Docile and Range.Value or math.huge, Root.Position, false)
TargetToMoveTo = Check ~= nil and Check or TargetToMoveTo
end
end)
local function Main(Position, Part:BasePart)
if Position then
local Mag = (Root.Position - Position).Magnitude
if Mag > Range.Value or Part and not CheckSight(Part) then
local Success = pcall(function() Path:ComputeAsync(Root.Position, Position) end) -- the current assumption is that waypoints are causing high memory usage
local CurrentIndex = 0
if Success and Path and Path.Status == Enum.PathStatus.Success then
--print("Success")
Pathing = true
Blocked = false
BlockConn = Path.Blocked:Connect(function(BlockedIndex) -- Make this a one time connection from what i figured
if BlockedIndex >= CurrentIndex then
print("Lol blocked")
Blocked = true
end
end)
local Points:{PathWaypoint} = Path:GetWaypoints()
if DebugMode then
DebugPath(Points)
end
task.spawn(function()
for i, v in Points do
if not Active.Value or Humanoid:GetState() == Enum.HumanoidStateType.Dead or not Pathing then
break
end
print("Waypointed")
Waypoint = v
CurrentIndex = i
task.wait((16/Humanoid.WalkSpeed)/4)
end
end)
else
print("Path creation unsuccessful")
end
end
end
end
task.spawn(function() -- Make Path
repeat
if TargetToMoveTo and not CheckSight(TargetToMoveTo) then
--if Main() then
Main(TargetToMoveTo.Position, TargetToMoveTo)
local Waits = 0
local Sight
repeat
Sight = CheckSight(TargetToMoveTo)
Waits += task.wait(.2)
print("waiting")
until Waits >= 2 or Blocked or Sight
if BlockConn then
BlockConn:Disconnect()
end
print("resetting")
--if Path then
-- Path:Destroy() -- Destroy it every 2 seconds and create a new one
-- task.wait()
-- Path = nil
Waypoint = nil
--end
--end
Pathing = false
else
task.wait()
end
until Humanoid:GetState() == Enum.HumanoidStateType.Dead
end)
local function Clean()
Waypoint = nil
for i, v in VisibleWaypoints do
v:Destroy()
end
table.clear(VisibleWaypoints)
if Path then
Path:Destroy()
end
Path = nil
end
Humanoid.Died:Connect(Clean)
Character.Destroying:Connect(Clean)