X can not be assigned to

Hello anyone
I am making the first-person camera using a part and weld attached to head
but the ERROR happens it says X can not be assigned to so how to bypass it I know it because Roblox here my code if you need it

local CamPart = script.Parent
local player = game:GetService("Players")
local Charater = player.LocalPlayer.Character
local Add = CamPart.AddCFrame

local function SetupWeld()
	CamPart.Anchored = false
	local Head = Charater:WaitForChild("Head")
	local Weld = CamPart.Face
	Weld.Part1 = Head
end

local function SetupCam()
	CamPart.Anchored = false
	local Head = Charater:WaitForChild("Head")
	local Weld = CamPart.Face
	Weld.Enabled = false
	
	CamPart.CFrame.Position.X = Head.CFrame.Position.X + Add.Value.Position.X
	CamPart.CFrame.Position.Y = Head.CFrame.Position.Y + Add.Value.Position.Y
	CamPart.CFrame.Position.Z = Head.CFrame.Position.Z + Add.Value.Position.Z
	
	CamPart.CFrame.Rotation.X = Head.CFrame.Rotation.X + Add.Value.Rotation.X
	CamPart.CFrame.Rotation.Y = Head.CFrame.Rotation.Y + Add.Value.Rotation.Y
	CamPart.CFrame.Rotation.Z = Head.CFrame.Rotation.Z + Add.Value.Rotation.Z
	
	Weld.Enabled = true
	
end

local EventSwitch = game.ReplicatedStorage.Events.CamSwitch

EventSwitch.Event:Connect(function()
	SetupWeld()
	wait(0.1)
	SetupCam()
end)

The ERROR is started at line 19
I Can’t do something else because I have to + Head Position with Offset Value Called Add
If you need Other Info Reply me

Yeah, vectors are created as read-only, and as such cannot be manipulated. Likewise, it’s not possible to directly change one of the vectors of a CFrame. We have to have to reconstruct these datatypes to apply modifications. This is understandably difficult to notice, since the mathematical operations performed on these datatypes return entirely new objects.

@AskWisp recreate it like this

local CamPart = script.Parent
local player = game:GetService("Players")
local Charater = player.LocalPlayer.Character
local Add = CamPart.AddCFrame

local function SetupWeld()
	CamPart.Anchored = false
	local Head = Charater:WaitForChild("Head")
	local Weld = CamPart.Face
	Weld.Part1 = Head
end

local function SetupCam()
	CamPart.Anchored = false
	local Head = Charater:WaitForChild("Head")
	local Weld = CamPart.Face
	Weld.Enabled = false
	
	local PX = Head.CFrame.Position.X + Add.Value.Position.X
	local PY = Head.CFrame.Position.Y + Add.Value.Position.Y
	local PZ = Head.CFrame.Position.Z + Add.Value.Position.Z
	
	local RX = Head.CFrame.Rotation.X + Add.Value.Rotation.X
	local RY = Head.CFrame.Rotation.Y + Add.Value.Rotation.Y
	local RZ = Head.CFrame.Rotation.Z + Add.Value.Rotation.Z
	
	CamPart.CFrame.Position = Vector3.new(PX, PY, PZ)
	CamPart.CFrame.Rotation = Vector3.new(RX, RY, RZ)
	
	Weld.Enabled = true
	
end

local EventSwitch = game.ReplicatedStorage.Events.CamSwitch

EventSwitch.Event:Connect(function()
	SetupWeld()
	wait(0.1)
	SetupCam()
end)

now it says Position cannot be assigned to

CFrame.Position is only the first vector within the larger structure that is the CFrame matrix. If you want to replace it, you could rebuild the CFrame as so:

local NewPosition = Head.CFrame.Position + Add.Value.Position
local LastCFrame = CamPart.CFrame
CamPart.CFrame = CFrame.fromMatrix(
	NewPosition,
	LastCFrame.XVector,
	LastCFrame.YVector,
	LastCFrame.ZVector
)

Lemme try it
wait for me please~

1 Like

Hey I Couldn’t understand
Here is what I want Position and Rotation
and so don’t you mind sharing the template of CFrame.fromMatrix, please
sorry I’m a beginner or I will try my best to understand it

Oh, I see now. I misread. I thought you meant to preserve the old orientation and only replace the position. What exactly are you hoping to accomplish then? I’m not so sure that the current algorithm as you propose it would even be the correct implementation of what you desire.

Assuming that it was, the Rotation property of CFame returns a new CFrame that only has the rotational component (the last three vectors). CFrame.fromMatrix simply constructs a new CFrame from these four vectors.

Wait I found out I don’t really use rotation I only need Position Lemme Fix it

@AskWisp Hey GoodNews It work!!!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.