Camera script not working

Hello, I currently have a script that limits a player’s rotation of the first-person camera on the Y-axis. It does this well, however it additionally limits rotation on the X-axis which I do not want it to do. I have analyzed the script and don’t know how I can write it so that only the Y-axis gets limited.

local RunService = game:GetService("RunService")
local Camera = workspace.CurrentCamera

RunService.RenderStepped:Connect(function()
	local Cam = Camera.CFrame:ToOrientation()	
	local Val = math.clamp(math.deg(Cam), -50, 100)
	Camera.CFrame = CFrame.new(Camera.CFrame.p) * CFrame.fromOrientation(math.rad(Val), math.rad(170), 0)	
end)

That part of the fromOrientation function is the X axis. Meaning, you are limiting the X axis because you are changing it to math.rad(Val)

As you see here, the X-axis is the first parameter.
image

Hey, I get that but what would I have to change to fix it? Do I make the Y-axis equal to math.rad(Val) and set the X-axis to 0?

You would want to keep the X axis the same as it was (don’t change it at all), meaning you would set it to Cam.X I believe.

Apologies, it would not be Cam.X

Try this code:

local RunService = game:GetService("RunService")
local Camera = workspace.CurrentCamera

RunService.RenderStepped:Connect(function()
	local Cam = table.pack(Camera.CFrame:ToOrientation()) -- Order is Z,X,Y	
	local Val = math.clamp(math.deg(Cam[3]), -50, 100)
	Camera.CFrame = CFrame.new(Camera.CFrame.p) * CFrame.fromOrientation(Cam[2], math.deg(Val), Cam[1])	
end)

Doesn’t work. It results in the camera not moving at all.

Are you sure it’s the Y-axis you want to limit?

Yes, It is. This is because my game has a script where you can see your own body. If you look down all the way you can see the top of your torso which ruins the effect (see image). I want to stop the camera before then.

image

Okay, so allow me to elaborate on a few things for clarity.

The code you originally sent does a few things. First of all, ToOrientation() returns a tuple in the order of (Z-axis, X-axis, Y-axis). When you make the Val variable, math.deg(Cam) is equal to the degrees of the Z-axis due to it being the first number in the tuple. Val is now equal to a limitation of the Z-axis between -50 and 100.

That isn’t the only issue. It then changes the CFrame orientation of the camera by first getting the original CFrame, then creating a fromOrientation CFrame. Which is correct. The first parameter of the fromOrientation function is the X-axis. In your code, the X-axis set to the radian of the limited Z-axis (variable Val). The Y-axis is set to a constant 170 degrees, and does not change no matter what, due to it being a constant number. The Z-axis is set to 0 radians, which is also constant (obviously).

Therefore, the original code only changes the X-axis of the camera’s CFrame. The Y and Z axes were not touched. It was never limiting the Y-axis.

Nevermind, I made it work. Thanks for the help!