How to knockback a player opposite to their LookVector?

I want a knockback system whenever you get damaged. For this, I’m using BodyVelocity (I know it’s deprecated; you can suggest other alternatives if you want) but I don’t understand how to knock a player in the opposite direction they’re facing.
I just need to know how to calculate the velocity.

While using a BodyVelocity would work, I would personally use :ApplyImpulse() on the character’s HumanoidRootPart.

local character = game.Workspace.TurboGoStu

-- We can get the direction the character is facing by getting the LookVector of their HumanoidRootPart.
local rootPart = character.PrimaryPart
local facingDirection = rootPart.CFrame.LookVector -- This is a unit vector.

-- Play around with this value to get a result you're happy with.
local force = 100 * rootPart.AssemblyMass

-- Apply an impulse to the HumanoidRootPart in the opposite direction to where they're facing.
rootPart:ApplyImpulse(-facingDirection * force)

The character will appear to slide backwards, so you could also integrate some kind of force to push the character up as they’re knocked back.

I hope the comments are clear enough to understand what the code is doing.

1 Like

Now the character doesn’t even move.

Ensure that you run this code on the client. I should have clarified, sorry.

This is me running it from the command bar on the client.

1 Like

Thanks! I’ll keep this in mind.

1 Like

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