There is an issue with the Camera, urgent help

When I tilt my camera downwards, the character turns upside down

Video:

External Media

Code:

local Settings : SettingsType = require(script:WaitForChild("Settings"))

type SettingsType = {
	BaseRot : number,
	SetRot : number,
	BaseFreq : number,
	SetFreq : number,
	BaseMult : number,
	BaseNumLerp: number,
	SwayStrenght : number,
	BaseSway : number,
	SetSway : number,
	CustomSwayZVal : number,
	DriftMin : number,
	DriftMax : number,
	Rate : number,
	MaxBlur : number,
	BlurMult : number,
}

local sway : CFrame
local MouseDelta : Vector2
local Vel,t,x,y : number

local sin = math.sin
local cos = math.cos
local rad = math.rad
local abs = math.abs
local sqrt = math.sqrt
local clamp = math.clamp
local round = math.round
local clock = os.clock

local CurrentVel = 0
local Drift = 0
local Limiter = 0

local RunServ = game:GetService("RunService")
local UIS = game:GetService("UserInputService")
local PlrServ = game:GetService("Players")
local Workspace = game:GetService("Workspace")
local LightingServ = game:GetService("Lighting")

local LocalPlr : Player = PlrServ.LocalPlayer

local Cam : Camera = Workspace.CurrentCamera
local BaseFov = Cam.FieldOfView

Cam.CameraType = Enum.CameraType.Custom
LocalPlr.CameraMode = Enum.CameraMode.LockFirstPerson

local char : Model = LocalPlr.Character or LocalPlr.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid") :: Humanoid
local HRP = char:WaitForChild("HumanoidRootPart") :: BasePart

local self,player = script.Parent,game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:wait()
local humanoid = char:WaitForChild("Humanoid") 



function antiTrans(part)
	if part and part:IsA("BasePart") and( part.Name=="Left Arm" or part.Name=="Right Arm" or part.Name=="Torso" or part.Name=="Right Leg" or part.Name=="Left Leg") then -- checks if a part and is a arm
		part.LocalTransparencyModifier = part.Transparency
		part.Changed:connect(function (property)    
			part.LocalTransparencyModifier = part.Transparency
		end)
	end
end

for _,v in pairs(char:GetChildren()) do
	antiTrans(v) 
end

function NumLerp(num1: number, num2: number, rate: number) : number
	return num1 + (num2-num1)*rate
end

function CalculateCurve(Base : number, Set : number) : number
	return sin(clock() * Base) * Set
end

function GetVelMag() : number
	return round(Vector3.new(HRP.AssemblyLinearVelocity.X, HRP.AssemblyLinearVelocity.Y, HRP.AssemblyLinearVelocity.Z).Magnitude)
end

function GetMouseDrift(Drift : number, MouseDelta : Vector2, dt : number) : number
	return NumLerp(Drift, clamp(MouseDelta.X, Settings.DriftMin, Settings.DriftMax), (Settings.BaseMult * dt))
end

function GetSwayVal(x:number, y:number) : CFrame
	return CFrame.new(Vector3.new(x, y, 0), Vector3.new(x*.95, y*.95, Settings.CustomSwayZVal)) + Cam.CFrame.Position
end

local function setBlur() : BlurEffect
	local MotionBlur : BlurEffect = LightingServ:FindFirstChild("MotionBlur")
	
	if not MotionBlur then
		local newMotionBlur = Instance.new("BlurEffect")
		newMotionBlur.Size = 0
		newMotionBlur.Name = 'MotionBlur'
		newMotionBlur.Enabled = true
		newMotionBlur.Parent = LightingServ
		
		MotionBlur = newMotionBlur
	end
	
	return MotionBlur
end

local MotionBlur = setBlur()

function ConvCFrameToOrientation(_CFrame: CFrame)
	local setX, setY, setZ = _CFrame:ToOrientation()
	return Vector3.new(math.deg(setX), math.deg(setY), math.deg(setZ))
end

local function CameraUpdt(dt)
	Limiter += dt
	if Limiter >= 1/Settings.Rate then
		t = clock()
		MouseDelta = UIS:GetMouseDelta()
		Vel = NumLerp(CurrentVel, GetVelMag(), Settings.BaseNumLerp)
		
		x = cos(t * Settings.BaseSway) * Settings.SetSway
		y = sin(t * Settings.BaseSway) * Settings.SetSway 
		sway = GetSwayVal(x,y)
		
		Drift = GetMouseDrift(Drift, MouseDelta, dt)

		Cam.FieldOfView = BaseFov + sqrt(Vel)
		
		MotionBlur.Size = clamp(abs(Drift*Settings.BlurMult) --[[ & Vel with lower mult]], 0, Settings.MaxBlur)
		
		CurrentVel = Vel
		Cam.CFrame = Cam.CFrame--:Lerp(sway * Cam.CFrame.Rotation, Settings.SwayStrenght)
			* CFrame.new(0, 1 + CalculateCurve(Settings.BaseFreq, Settings.SetFreq) * Vel / Settings.BaseMult, -0.5)
			* CFrame.Angles(0, 0, rad(CalculateCurve(Settings.BaseRot, Settings.SetRot) * Vel / Settings.BaseMult) + rad(Drift))
		
		hum.CameraOffset = ConvCFrameToOrientation(sway)
		
		Limiter -= 1/Settings.Rate
	end
end

RunServ:BindToRenderStep("CamEffect", Enum.RenderPriority.Camera.Value, CameraUpdt)

local function reset() : ()
	LocalPlr.CameraMode = Enum.CameraMode.Classic
	--Cam.CameraType = Enum.CameraType.Custom
end

--hum.Died:Once(reset)

I think the problem is here, but I don’t know how to fix it.

Cam.CFrame = Cam.CFrame--:Lerp(sway * Cam.CFrame.Rotation, Settings.SwayStrenght)
			* CFrame.new(0, 1 + CalculateCurve(Settings.BaseFreq, Settings.SetFreq) * Vel / Settings.BaseMult, -0.5)
			* CFrame.Angles(0, 0, rad(CalculateCurve(Settings.BaseRot, Settings.SetRot) * Vel / Settings.BaseMult) + rad(Drift))