So i just found out the other day that the AI of enemies in a game i’m trying to make is causing memory leakages and lag. These memory leakages would appear as spikes in memory instead of gradual increases in memory. I think i need help with solving this with someone in studio because this problem is so hard for me to fix let alone completely find.
I’ve tried reducing the interval of which a path is computed.
And the lag also occurs even when less than 20 enemies are present. in fact the game had lag spikes like this when there was just 1 - 11 enemies on the map. i don’t know why this is happening but it’s not fun to deal with.
The npcs pathfinding would also have trouble using their pathfinding as they would often be ramming towards a wall, which i think is a result of the lag.
I’ll show code snippets here
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
Path = PathfindingService:CreatePath(table.unpack(PathParams))
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
Path.Blocked:Connect(function(BlockedIndex) -- Make this a one time connection from what i figured
if BlockedIndex >= CurrentIndex then
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 Path then
break
end
Waypoint = v
CurrentIndex = i
task.wait((16/Humanoid.WalkSpeed)/4)
end
end)
else
print("Path creation unsuccessful")
end
end
end
end
local function Clean()
Waypoint = nil
for i, v in VisibleWaypoints do
v:Destroy()
end
table.clear(VisibleWaypoints)
if Path then if Path then Path:Destroy() end end
Path = nil
end
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)
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)
until Waits >= 2 or Blocked or Sight
if Path then
if Path then Path:Destroy() end -- Destroy it every 2 seconds and create a new one
Path = nil
Waypoint = nil
end
--end
end
task.wait(.2)
until Humanoid:GetState() == Enum.HumanoidStateType.Dead
end)
Humanoid.Died:Connect(Clean)
Character.Destroying:Connect(Clean)
repeat
if not Active.Value then
Humanoid:MoveTo(Root.Position)
Active.Changed:Wait()
end
local Interval = (16/Humanoid.WalkSpeed)/4
if TargetToMoveTo then
local Magnitude = (TargetToMoveTo.Position - Root.Position).Magnitude
local Height = TargetToMoveTo and (TargetToMoveTo.Position.Y - Root.Position.Y)
local MoveMethod = TargetToMoveTo.Position
if Mode.Value == 2 then
if Magnitude < StopAt.Value then
MoveMethod = Root.Position
end
elseif Mode.Value == 3 then
if Magnitude < StopAt.Value and Magnitude > StopAt.Value/4 then
MoveMethod = TargetToMoveTo.Position
+(Vector3.new(math.random()-.5,0,math.random()-.5).Unit
*math.abs(math.min((TargetToMoveTo.Position-Root.Position).Magnitude-Range.Value,0)))
Interval = math.random(1, 2) / 2
elseif Magnitude < StopAt.Value/4 then
MoveMethod = CFrame.new(TargetToMoveTo.Velocity):Inverse().Position
end
if Height > HeightLimit then
if not CheckSight(TargetToMoveTo, Range.Value + Height * 2) then
MoveMethod = TargetToMoveTo.Position + (Vector3.new(math.random(-1, 1) * Range.Value, 0, math.random(-1, 1) * Range.Value) * (TargetToMoveTo.Position - Root.Position).Magnitude) * Height
Interval = math.random(1, 3) / 2
else
MoveMethod = Root.Position
end
end
end
if Waypoint and Waypoint.Action == Enum.PathWaypointAction.Jump or Height >= HeightLimit/2 and Height < HeightLimit * 2 then
Humanoid.Jump = true
end
Humanoid:MoveTo(Waypoint and Waypoint.Position or MoveMethod, TargetToMoveTo)
GETOUTTAMYWAY()
end
task.wait(Interval)
until Humanoid:GetState() == Enum.HumanoidStateType.Dead
And these images are the proof
The memory leakages present as spikes that do not go down instead of the typical gradual memory leakage.
The lag was persistent even with very few npcs