Invalid argument #2 (CFrame expected, got Vector3)?

Hey, I’m trying to point an object in a certain direction, three studs in front of my player to be specific, so it can move in front of them. But no matter what I change in (what I think is) argument two, it gives me the same error.
arm["Right Arm"].CFrame = CFrame.new(arm["Right Arm"].CFrame:ToWorldSpace(Vector3.new(0,0,-1)),arm["Right Arm"].CFrame.LookVector) is the code that’s been giving me issues.

Now I know there might be similar topics, but what differentiates this one from the others is:

  1. Argument two is invalid, not argument one.
  2. It expects a CFrame.

What am I doing wrong with my code?

arm["Right Arm"].CFrame = arm["Right Arm"].CFrame:ToWorldSpace(CFrame.new(0,0,-1))

Maybe this will work, I don’t know why you added CFrame.LookVector at the end though

So I can point the arm at a specific point!

Argument 2 is arm[“Right Arm”].CFrame.LookVector. It is a Vector3, not a CFrame, just use:
CFrame.new(arm[“Right Arm”].CFrame.LookVector) I think. That’ll convert it to a CFrame.\

From the name LookVector

CFrame CFrame:ToWorldSpace ( CFrame cf )

The issue is in ToWorldSpace. It takes in a CFrame, not a Vector3.

Another issue would be that CFrame.new does not accept a CFrame at all for it’s arguments. ToWorldSpace returns a CFrame so you’d have to get the position of it.

Solving this probably won’t achieve what you want, but at least you won’t have errors.

CFrame.new(
  CFrame.new(0, 0, 0):ToWorldSpace(Vector3.new(0, 0, 0)),
  Vector3.new(0, 0, 0))

This is essentially your code and it gives me the same error: invalid argument #2 (CFrame expected, got Vector3)

1 Like

I tried that prior, gave same error.

Not the issue, that wasn’t what was causing the error.

That’s literally what is causing the error. I reproduced your error… Oh well, I’ll just believe you.

LookVector returns a Vector3 value if I am not mistaken. Try making a CFrame out of the x,y,z of the Vector3 value.

Psuedo code:

RightArm.CFrame = (CFrame : number, CFrame(x,y,z))

Not the issue, same error retrieved.

Mocking me?
Fine, would I have to use “.p” or would it be something else?

It says it needs CFrame instead of Vector3, I don’t know about ToWorldSpace, but it says Argument 2. LookVector is a Vector3 value.

CFrame.new(0, 0, 0):ToWorldSpace(Vector3.new(0, 0, 0))

That by itself gives the error:
invalid argument #2 (CFrame expected, got Vector3)

I assume it has to do with :. What’s actually happening is:

local a = CFrame.new()
a.ToWorldSpace(a, Vector3.new())

So you could call this a ROBLOX bug? Off by one error output?

Argument 2 means second thing in the brackets, that may be an error of itself. I don’t think the : is erroring.
The ToWorldSpace might be another error. But I do not believe it is the case here.

The only thing that has 2 arguments here is the CFrame.new constructor.

But you are right, that’s another thing that needs to be fixed.

Do you know how : works in relation to class methods? The code never reaches the second argument of his CFrame.new because it crashes at the incorrect usage of ToWorldSpace.

I understand if you two are confused because the error output is misleading, this is probably ROBLOX’s fault. Not sure how they ought to do different error output based on if you use the syntax sugar for class methods.

1 Like

I have never seen this behavior before but what I can actually tell you, is that I use Java and I checked something interesting not too long ago.

You might or might not know, but a Java array can’t have more indexes than the ones you set, and you have to set the type of the array.

So I did something like this:

String[] arr = new String[4] //create a new string array with 4 places.
arr[7] = 3 //I got out of bounds and set the wrong value type.

As I remember, the error I got was the wrong type, because I tried to be set an integer in a string array.

However, the first thing I did was to leave the bounds of the array, so wouldn’t it error first? Nope.

What I am trying to say here, is that even when the first thing I did was to leave the bounds, I got the type error instead.

It doesn’t necessarily stop by the order AFAIK and understand, but I might be wrong.

It doesn’t necessarily stop by the order AFAIK and understand, but I might be wrong.

Lua is an interpreted language meaning it runs line by line. Java is a compiled language, which reads the entire file and then creates code you can run. So when it was compiling, it “crashed” on the type error. Not sure if Java’s compiler checks for out of bounds access in arrays.

Edit: Some of what I said is incorrect

1 Like

the error in the code is caused by using vector3 instead of a cframe on CFrame:ToWorldSpace(CFrame) like Hamster said above

arm[“Right Arm”].CFrame = CFrame.new(arm[“Right Arm”].CFrame:ToWorldSpace(CFrame.new(0,0,-1)),arm[“Right Arm”].CFrame.LookVector) – changing it to a cframe

An easier way of doing this for your use I think would be something like this:

Part.CFrame = arm.CFrame – this gives it the same cframe rotation of the arm
Part.Position = arm.Position + (arm.CFrame.LookVector* 5) – this moves it 5 studs infrom of the arms lookvector

or with 1 line:
Part.CFrame = (arm.CFrame - arm.Position) + (arm.Position + (arm.CFrame.LookVector* 5))

1 Like

CFrame:ToWorldSpace() takes a CFrame and returns a CFrame, CFrame:PointToWorldSpace() takes a Vector3 and returns a Vector3.

Also second argument of CFrame.new() takes the Vector3 you should be looking at and not the look direction (LookVector), so here is a fix
arm["Right Arm"].CFrame = arm["Right Arm"].CFrame:ToWorldSpace(CFrame.new(Vector3.new(0, 0, -1), arm["Right Arm"].CFrame.LookVector))

also this method will keep on moving the arm infinitely forwards as you keep incrementing it, why do you want the hand to be moving forwards?

Okay, finally! Thank you! This finally makes the effect working properly, but I now need the effects pointing at a very specific location. How do you think I can achieve this?

Edit: I should probably elaborate. Here: