Trying to make a studio/in-game alike camera, but it rotates on the sides

You know how the camera behaves in the roblox studio, right?
I am trying to achieve it, however I’ve been asking around and experimenting for an entire day and I couldn’t get it to work. Here is the code that works with the CFrame of the camera:

local _uis = game:GetService("UserInputService")
mouseDelta = _uis:GetMouseDelta()
cam.CFrame = cam.CFrame * CFrame.Angles(math.rad(-mouseDelta.Y), math.rad(-mouseDelta.X), 0)

And as you can see on the video instead of acting like the ingame/studio camera it rotates on the sides (on the Z coordinate)


How can I make it so it doesn’t rotate on the sides?

Nevermind found the solution after so much time
Here’s a link to the solution that I found: How do I fix my camera CFrame rotation? - #10 by debugger57