AI Causing massive problems in game

I have been dealing with this problem for around 6 months now which is 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 which would happen for no apparent reason. 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.
Creating the path instance only once, then destroying it when it’s no longer needed, such as when the npc dies or is destroyed.

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.

To note, the map the enemies are placed on is super large, around 2 baseplates in length.

if anyone wants to point out how i’ve been copy and pasting posts like this, that isn’t necessary and that’s because i’m trying to get help like everyone else.

image
image
image
Cope Snippets:

-- 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)

Model