Cframe expected, got vector3

hello there, Im not the best with cframes and I am trying to make a knockback system. here is my script:

local Speed = 42
local Force = 80000
local Char

Char = Hit.Parent
Knockback.MaxForce = Vector3.new(Force, Force, Force)
Knockback.Velocity = script.Parent.Parent:FindFirstChild("HumanoidRootPart").CFrame.LookVector * CFrame.new(Vector3.new(Speed, 100, 0))

I am confused on why this throws the error Workspace.me7474.Sword.ToolHandler:78: invalid argument #1 (CFrame expected, got Vector3). the final line is 78, and I am providing it with a CFrame, am I not? when I do Knockback.Velocity = script.Parent.Parent:FindFirstChild("HumanoidRootPart").CFrame.LookVector * Speed it works, even though that is a integer, not a cframe. I am trying to make it knock you upwards as well as back. thanks in advance!

2 Likes

Instead of this just do

CFrame.new(Speed, 100, 0)

Im on phone so expect typos.

CFrames may multiply with other cframes and vector3. But order matters, Vector3*Cframe is not allowed, but the other way is. In your case, the LookVector is a Vector, and you multiply it with a CFrame. The order you did it is not allowed, and will error.

What you should do is remove the .LookVector and the CFrame.new. This makes CFrame multiply by Vector, not the other way

1 Like

I have tried this, it does not work.

If I remove the look vector, won’t it not hit them in the direction the player is facing? Sorry, I’m not the best at cframes :sweat_smile:

lookvector returns a Vector3 and not a CFrame, which means you’re using CFrame multiplied by a Vector3, so you’re not even using Vector3 math operations.

Knockback.Velocity = script.Parent.Parent:FindFirstChild("HumanoidRootPart").CFrame.LookVector + Vector3.new(Speed, 100, 0)
2 Likes

multiplying CFrame by vectors, changes position relative to the rotation. So lets say

Cframe.new(1,0,0)*Vector3.new(5,0,0)

It will move the cframe 5 to the X axis. But if the Cframe was facing backwards, it would move towards negative X.

Same with your code, it moves it speed amount of studs to the X axis.

Note: X is the “rightwards” direction of an axis
Y is the upwards; and
Z is the backwards
Notice that Z is backwards, and that is right. If you want it to face forward, you would need to make it go to negative Z, opposite of backwards which is forward.

In your case, since you want it to go forward (and up?), you would do this

Knockback.Velocity = script.Parent.Parent:FindFirstChild("HumanoidRootPart").CFrame * Vector3.new(0, 100, -Speed)

You noticed I typed -speed, and that is because Z axis goes backwards. You would have to reverse the direction

Try this:

local Speed = 42
local Force = 80000
local Char

Char = Hit.Parent
Knockback.MaxForce = Vector3.new(Force, Force, Force)
Knockback.Velocity = CFrame.new(script.Parent.Parent:FindFirstChild("HumanoidRootPart").CFrame.LookVector) * CFrame.new(Vector3.new(Speed, 100, 0))

This may not solve it, but do you need Vector3.new at the end inside of CFrame.new?

Don’t think so, it was in the original script though

Im guessing we dont need it and it may give an error

1 Like

This fixed it, thought LookVector returned a CFrame for some reason, even though it has vector in its name… thank you!

1 Like

sorry, @Adentified, it does not throw the error anymore, but it still does not hit the player upward. it hits them foreword as if the Y value was 0.

have you tried using BasePart:ApplyImpulse() instead? BasePart.Velocity is deprecated, and not only that, but I feel like utilizing ApplyImpulse will actually fix your issue

I will try it now, thanks!

edit: I can’t get it working, plus I tried changing the z value of the vector3 that is being added to the lookvector, and that does nothing. I think that only the x is working, as neither the Y or Z are doing anything.

I’ll help you, here:

Knockback:ApplyImpulse(script.Parent.Parent:FindFirstChild("HumanoidRootPart").CFrame.LookVector + Vector3.new(Speed, 100, 0)
-- I can guarantee the lookvector is the correct Vector3, keep messing with the second Vector3 until you get what is satisfactory for you`

I should have made this more clear in the parent post, but Knockback is a BodyVelocity. I am going to apply the impluse to the the characters HumanoidRootPart, as that is where Knockback is located.

edit: the script you provided causes the player to move upward slightly, but not backwards at all. it gives 0 errors.

That’s my bad. You should then apply it to the HumanoidRootPart if you haven’t done so already.

Also, it’s not an error, otherwise it would be throwing one. To be more specific. due to the HumanoidRootPart being a relatively big part (for apply impulse at least) it requires larger integers. I did say in my last post to mess with the second Vector3, enlarge the numbers for both speed and the y value, make sure to enlarge it by large jumps rather than counting up by 1 or 2. It’s really that simple.

when using impulse the character teleports, and does not smoothly fly through the air. :ApplyImpulse() does not seem to give me the look I want.

If the players teleporting you’re not using it right, do research on it

1 Like