-- Code to continue tracking & jumping on Active Mobs

task.defer(function()
	while true do
		task.wait(1/4)
		for MobInstance, Mob in ActiveMobs do
			task.spawn(function()
				if not Mob.isDead and not Mob.Destroyed then
					local Player, m = AI:GetClosestPlayer(Mob)
					local Enemy = Mob.Enemy
					
					-- Simulation
					if Mob.Root.Anchored then
						Mob.Root.Anchored = false
					end
					if not Mob.Root:GetNetworkOwner() then
						Mob.Root:SetNetworkOwner(Player)
						task.wait(0.05) -- Give physics more time so it doesn't appear as choppy
					end
					
					-- Tracking
					if Player and Enemy then
						Enemy:MoveTo(Player.Character:GetPivot().Position, Player.Character.PrimaryPart)
					else
						ActiveMobs[MobInstance] = nil
						Mob.Root:SetNetworkOwner()
						Enemy:MoveTo(Mob.Origin.Position)
					end
					
					-- Jumping
					AI:RequestJump(Mob)
				else
					ActiveMobs[MobInstance] = nil
				end
			end)
		end
	end
end)

return AI
