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!
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
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.
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
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
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.
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.