Assigning the player's camera to a new CFrame

I had a hard time following the documentation for CFrames. When I tried to implement a script to assign the player camera’s CFrame to a part’s (cam1/cam2) CFrame, it either comes up with “Position cannot be assigned to.” or “invalid argument #1 to ‘new’ (Vector3 expected, got CFrame)”.


The problematic code in question:

	if useCurrentCam then
		local CF = CFrame.new(workspace.CurrentCamera.CFrame)
        camera.CFrame = CF -- use player camera as cam1
	else
		local CF = CFrame.new(cam1.CFrame)
        camera.CFrame.Position = CF -- use set camera as cam1
    end

I’ve asked multiple people about this and apparently I’m just not getting it. :slight_smile:
The code in question is contained in a Modulescript under StarterGui if that helps

4 Likes

just put
camera.CFrame = CF
instead of
camera.CFrame.Position = CF

1 Like

CFrame.new() requires a vector3 to be used, you just need cam1.CFrame not to CFrame the existing cframe.
and like slav said you cannot set a cframes position like that.

1 Like

camera.CFrame = cam1.CFrame

cam1.CFrame is already a cframe

1 Like

if I do this it just sets the Vector3 position to (0,0,0) instead of returning an error

1 Like

that means cam1.CFrame is positioned at 0,0,0 or something else is changing it after

1 Like

script 1:

gameFunc.CamControl(workspace.cam2.CFrame, workspace.cam3.CFrame, false, true, 2, 2)

script 2:

if useCurrentCam then
		local CF = CFrame.new(workspace.CurrentCamera.CFrame)
        camera.CFrame = CF -- use player camera as cam1
	else
		camera.CFrame = cam1
    end

this doesnt return an error but also doesnt set the value. ive been told elsewhere that this is because it changes the vector3 of camera.CFrame to (0, 0, 0), but I had it print the CFrame value and the value is correct, but the player’s camera doesn’t migrate to the new CFrame value.

1 Like

it might be because its workspace.Camera

try doing

local camera = game.Workspace.CurrentCamera
2 Likes

no luck, and still no errors :frowning:
come to think of it let me try a localscript

put it in a localscript and not server

1 Like

I should have known my code was running serverside :sob:

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