Camera won't tilt

I am trying to make a script that makes the players camera tilt at an angle depending on if the players character is moving left or right (First Person), but anything I try just doesn’t seem to change the camera at all, why does it not work and what am I doing wrong?

local player = game.Players.LocalPlayer
local camera = game.Workspace.CurrentCamera
local humanoid = player.Character and player.Character:FindFirstChild("Humanoid")

local sensitivity = 0.1
local maxRotation = 5
local minRotation = -5

if humanoid then
	humanoid:GetPropertyChangedSignal("MoveDirection"):Connect(function()
		local moveDirection = humanoid.MoveDirection
		local rootPart = humanoid.RootPart
		if rootPart then
			local velocity = rootPart.Velocity
			local speed = velocity.Magnitude
			local zChange = speed * sensitivity

			zChange = math.clamp(zChange, minRotation, maxRotation)

			if moveDirection.X > 0 then -- Moving right
				camera.CFrame *= CFrame.Angles(0, 0, math.rad(zChange))
			elseif moveDirection.X < 0 then -- Moving left
				camera.CFrame *= CFrame.Angles(0, 0, math.rad(-zChange))
			end
		end
	end)
end
1 Like

Trying doing it in a render stepped loop, thats usually where you wanna do camera related cframe stuff