Invalid argument #2 (Vector3 expected, got CFrame)

I’m having trouble with this weird error. I heard that, to get a cframe RELATIVE to another cframe, you have to multiply the two like this

newcframe = relativecframe * originalcframe

where “relativecframe” is the cframe you’re trying to place “originalcframe” relative to.

I’m trying to do this on this line of code:

cameraholder.CFrame += cameraholder.CFrame * nextMovement()

it’s not working. nextMovement() returns a vector3 value. here’s how it looks:

function nextMovement()
	local nextMove = Vector3.new()
    if uis:IsKeyDown("A") or uis:IsKeyDown("Left") then
        nextMove += Vector3.new(-1,0,0)
    end
    if uis:IsKeyDown("D") or uis:IsKeyDown("Right") then
        nextMove += Vector3.new(1,0,0)
	end
    if uis:IsKeyDown("W") or uis:IsKeyDown("Up") then
        nextMove += Vector3.new(0,0,-1)
	end
    if uis:IsKeyDown("S") or uis:IsKeyDown("Down") then
        nextMove += Vector3.new(0,0,1)
	end
    if uis:IsKeyDown("E") then
        nextMove += Vector3.new(0,1,0)
    end
    if uis:IsKeyDown("Q") then
        nextMove += Vector3.new(0,-1,0)
	end
    return nextMove * currentspeed
end

currentspeed is an integer and cameraholder is a part.

Someone help me with this, I’m getting tired of errors.

Try replacing

cameraholder.CFrame += cameraholder.CFrame * nextMovement()

with

cameraholder.CFrame += cameraholder.Position * nextMovement()

I tried this, and now it just doesn’t move at all and doesn’t error.

What line is the error occuring on?

Here’s the entire script, I’ve been changing it up trying to solve the error:

(local script)

local camera = game.Workspace.CurrentCamera
local player = game.Players.LocalPlayer
local rs = game:GetService("RunService")
local uis = game:GetService("UserInputService")
 
local selected = false
local originalspeed = 10
local currentspeed = originalspeed

repeat
	wait(0.05)
until player.Character

local char = player.Character
local cameraholder = Instance.new("Part", char)

cameraholder.Name = player.Name .. "'s Camera"
cameraholder.CanCollide = false
cameraholder.Position = game.Workspace.Center.Position
cameraholder.Transparency = 1
cameraholder.Anchored = true
cameraholder.CastShadow = false
cameraholder.Size = Vector3.new(1, 1, 1)
print('a')

camera.CameraSubject = cameraholder

print("1")

function nextMovement()
	local nextMove = Vector3.new()
    if uis:IsKeyDown("A") or uis:IsKeyDown("Left") then
        nextMove += Vector3.new(-1,0,0)
    end
    if uis:IsKeyDown("D") or uis:IsKeyDown("Right") then
        nextMove += Vector3.new(1,0,0)
	end
    if uis:IsKeyDown("W") or uis:IsKeyDown("Up") then
        nextMove += Vector3.new(0,0,-1)
	end
    if uis:IsKeyDown("S") or uis:IsKeyDown("Down") then
        nextMove += Vector3.new(0,0,1)
	end
    if uis:IsKeyDown("E") then
        nextMove += Vector3.new(0,1,0)
    end
    if uis:IsKeyDown("Q") then
        nextMove += Vector3.new(0,-1,0)
	end
    return nextMove
end

print("2")

function move()
	local lastUpdate = tick()
	while selected do
		rs.Heartbeat:Wait()
		lastUpdate = tick() - lastUpdate
		cameraholder.CFrame += cameraholder.CFrame * CFrame.new(nextMovement(lastUpdate) * lastUpdate * currentspeed) 
	end
end

print('3')

-- shift to speed

uis.InputBegan:Connect(function(input, gpe)
	if not gpe then
		if input.KeyCode == Enum.KeyCode.LeftShift then
			currentspeed = originalspeed * 2
		end
	end
end)
uis.InputEnded:Connect(function(input, gpe)
	if not gpe then
		if input.KeyCode == Enum.KeyCode.LeftShift then
			currentspeed = originalspeed
		end
	end
end)

print("3")

-- move

uis.InputBegan:Connect(function(input, gpe)
	if not gpe then
		if input.UserInputType == Enum.UserInputType.Keyboard then
			if nextMovement() ~= 0 then
				selected = true
				print("lol")
				move()
				print("1234")
			end
		end
	end
end)
uis.InputEnded:Connect(function(input, gpe)
	if not gpe then
		if input.UserInputType == Enum.UserInputType.Keyboard then
			if nextMovement() ~= 0 then
				selected = false
			end
		end
	end
end)

and what line is the actual error occuring on?

The line the error is occurring on is line 93.

What line is line 93!??? Can you copy and paste that line into the reply of this message

cameraholder.CFrame += cameraholder.CFrame * CFrame.new(nextMovement(lastUpdate) * lastUpdate * currentspeed)

if you replace

cameraholder.CFrame += cameraholder.CFrame * CFrame.new(nextMovement(lastUpdate) * lastUpdate * currentspeed)

with

cameraholder.CFrame += cameraholder.CFrame * nextMovement(lastUpdate) * lastUpdate * currentspeed

does anything happen?

uh yeah… it’s weird… I can only look up and down?

So I’m not really sure what you’re trying to do result wise… however, I do know what’s causing the error. The error is caused because you’re trying to do CFrame + CFrame when you should really be doing CFrame * CFrame (in this case that’d be *= instead of +=). However, there’s a lot to unpack with what you’re doing wrong. What you’re doing right now with the quoted snippet above is something like this:

currentCFrame = currentCFrame * (currentCFrame + movementVector3)

I doubt you’re wanting to multiply the current cameraHolder CFrame twice. Not only that, but I believe you’re wanting to move relative to the CFrame, so don’t quote me on this, but I think you want to be multiplying the nextMovement Vector3 converted to a CFrame to the cameraHolder CFrame. In other words, something like this instead:

cameraholder.CFrame *= CFrame.new(nextMovement())

EDIT: I changed what I was saying in my explanation

1 Like

You could simply add the vector3 onto the cframe lol.

local cfra = CFrame.new(0, 1, 0)
cfra += Vector3.new(1, 0, 1)
print(cfra) -- 1 1 1
function nextMovement(dtime)
	local nextMove = Vector3.new()
    if uis:IsKeyDown("A") or uis:IsKeyDown("Left") then
        nextMove += Vector3.new(-1,0,0)
    end
    if uis:IsKeyDown("D") or uis:IsKeyDown("Right") then
        nextMove += Vector3.new(1,0,0)
	end
    if uis:IsKeyDown("W") or uis:IsKeyDown("Up") then
        nextMove += Vector3.new(0,0,-1)
	end
    if uis:IsKeyDown("S") or uis:IsKeyDown("Down") then
        nextMove += Vector3.new(0,0,1)
	end
    if uis:IsKeyDown("E") then
        nextMove += Vector3.new(0,1,0)
    end
    if uis:IsKeyDown("Q") then
        nextMove += Vector3.new(0,-1,0)
	end
	return nextMove * (currentspeed * dtime)
end

print("2")

function move()
	local lastUpdate = tick()
	while selected do
		rs.Heartbeat:Wait()
		lastUpdate = tick() - lastUpdate
		cameraholder.CFrame *= cameraholder.CFrame * nextMovement(lastUpdate)
	end
end

This is how it looks now… but it still errors. This is the error:
Players.Den_vers.PlayerScripts.MovementScript:50: attempt to perform arithmetic (mul) on number and nil
It’s refering to the return statement in the nextMovement()

I’m trying to add the vector3 value relative to the orientation of the original cframe that is current camera

I highly recommend reading my reply again since I’ve changed it to be more understandable, but by adding a Vector3 to a CFrame, you’re transforming the CFrame in world space by the Vector3.

If you want to transform the CFrame by the Vector3 relative to the CFrame, you’ll need to convert the Vector3 to a CFrame.

Yes, I want to conserve orientation, so I should add a vector3 to the original CFrame to converse orientation like this?:

newcframe = originalcframe * CFrame.new(vector3)

and this should add a vector3 position on to the position of the cframe relative to it’s position and orientation, right?

That’s how you’d add a Vector3 relative to a CFrame, yes.

It’s no longer giving me the cframe error, but I keep getting this error:

Players.Den_vers.PlayerScripts.MovementScript:45: attempt to perform arithmetic (mul) on number and nil

here’s the script:

function nextMovement(dtime)
	local nextMove = Vector3.new()
    if uis:IsKeyDown("A") or uis:IsKeyDown("Left") then
        nextMove += Vector3.new(-1,0,0)
    end
    if uis:IsKeyDown("D") or uis:IsKeyDown("Right") then
        nextMove += Vector3.new(1,0,0)
	end
    if uis:IsKeyDown("W") or uis:IsKeyDown("Up") then
        nextMove += Vector3.new(0,0,-1)
	end
    if uis:IsKeyDown("S") or uis:IsKeyDown("Down") then
        nextMove += Vector3.new(0,0,1)
	end
    if uis:IsKeyDown("E") then
        nextMove += Vector3.new(0,1,0)
    end
    if uis:IsKeyDown("Q") then
        nextMove += Vector3.new(0,-1,0)
	end
	local scale = currentspeed * dtime
	return nextMove * scale
end

print("2")

function move()
	local lastUpdate = tick()
	while selected do
		rs.Heartbeat:Wait()
		lastUpdate = tick() - lastUpdate
		print(lastUpdate)
		print "asdf"
		cameraholder.CFrame = cameraholder.CFrame * nextMovement(lastUpdate)
		print "zxcv"
	end
end

I have absolutely no clue what line 50 is based on that snippet (it’s not the full script, so the line number is irrelevant unless you specify which line is line 50), so I can’t even know for sure what’s causing that specific error.

However, you’re still not converting the Vector3 returned by nextMovement to a CFrame.

nextMovement(lastUpdate)

should be:

CFrame.new(nextMovement(lastUpdate))