Connected and Autonomous Vehicles (CAVs) enable various capabilities and functionalities like automated driving assistance, navigation and path planning, cruise control, independent decision making, and low-carbon transportation in the real-time environment. However, the increased CAVs usage renders the potential vulnerabilities in the Internet of Vehicles (IoV) environment, making it susceptible to cyberattacks. An Intrusion Detection System (IDS) is a technique to report network assaults by potential Autonomous Vehicles (AVs) without encryption and authorization procedures for internal and external vehicular communications. This paper proposes an Intelligent IDS (IIDS) to enhance intrusion detection and categorize malicious AVs using a modified Convolutional Neural Network (CNN) with hyperparameter optimization approaches for IoV systems. The proposed IIDS framework works in a 5G Vehicle-to-Everything (V2X) environment to effectively broadcast messages about malicious AVs. Thus IIDS aids in preventing collisions and chaos, enhancing safety monitoring in the traffic. The experimental results depict that the proposed IIDS achieves 98% accuracy in detecting attacks.