"Y cannot be assigned to" error when using inequality with player's camera

Hello.

I’ve been having some trouble with a camera angle-detection system I have. Here is my code:
(LocalScript)

local cam = workspace.CurrentCamera
local ccflv = cam.CFrame.LookVector
if ccflv.Y > .8 then ccflv.Y = .8 end
		if ccflv.Y < -.8 then ccflv.Y = -.8 end

Whenever the player is looking directly up, I get the error that “Y cannot be assigned to”

image

I’ve looked on the forums but all I’ve seen is people getting this error when using positions. We’re talking inequalities here.

I’m not sure if it’s possible to do a comparison with Vector3 values and even if it is I’m clueless on where I’d even begin with that.

Help would be appreciated.

It’s because ccflv.Y is a read-only property of the LookVector (Vector3) you can’t modify individual components of a Vector3 directly, we need to create a new Vector3

local cam = workspace.CurrentCamera
local ccflv = cam.CFrame.LookVector
local y = math.clamp(ccflv.Y, -0.8, 0.8)
local newLookVector = Vector3.new(ccflv.X, y, ccflv.Z)

clamps the Y component of the LookVector between -0.8 and 0.8, creates a new vector with the adjusted Y

Small correction, you can do operations on vectors themselves to avoid needing to create a new one.

ccflv += Vector3.new(0, -ccflv.Y + 0.8, 0)
1 Like