Damage knockback

I have my own custom damage and HP system, and I want players to be knocked back to the opposite direction they’re facing so they can’t just touch the thing until they die instantly. I don’t know how to go about this though.

1 Like

If your damage system doesn’t already have some sort of cooldown, you would want to add one, even if it’s extremely temporary. Something like 0.3 seconds should ideally work, but you can change the number to whatever you think would be more suitable for your situation. This is basically just to prevent the damage from being activated multiple times while the character is still being bounced back.

To knock players backwards according to where they are facing, you can use the root part’s CFrame LookVector:

character.HumanoidRootPart.CFrame.LookVector

This is a Vector3 value with a magnitude of 1 that describes the direction the root part is facing. This should be all the data you need, because you can simply multiply this vector with a number to change its magnitude if needed.

The type of force you use to push them back is your choice. I think the most common method would be to use a temporary BodyMover such as BodyForce or BodyVelocity, then removing them shortly afterwards. I would personally use BodyVelocity because it’s more consistent from my experience, but you could likely get BodyForce to work fine as well, so it’s up to you.

1 Like

I’ll try this. How exactly would I have this push them backward, I was thinking at sort of an upward angle

1 Like

Thank you! This isnt related to this post but this helped, if you want you can post the same thing on my newest post and ill mark it as the soloution. <3 (I had the exact same question but just which method i should use.)

2 Likes

Do the lookvector + Vector3.new(0,number,0)
the higher the number the further it’ll go up

1 Like

So basically, when you insert a BodyMover into a part, it moves them according to the BodyMover’s properties.

For instance, BodyVelocity’s Velocity property will control the velocity of the part it’s inside of. You would probably want to change its MaxForce property as well because otherwise the speed that they get bounced back might be too limited. A common practice is to completely override the MaxForce by setting it to infinity:

BodyVelocity.MaxForce = Vector3.new(1,1,1) * math.huge 

Then, you can just worry about changing the Velocity property. In this case, you would want to set it to the LookVector I mentioned before. You would also want to multiply it by a negative magnitude. It has to be negative since you want to push them backwards, but the strength of it is up to you:

BodyVelocity.Velocity = RootPart.CFrame.LookVector * -20 

After this, you would want to remove it shortly afterwards so it doesn’t keep moving the character back forever. You can technically use something along the lines of a coroutine to do this, but you can also achieve this a lot simpler with the Debris service:

Debris:AddItem(BodyVelocity,0.4)  --is removed in 0.4 seconds

Again, you don’t have to use BodyVelocity if you want to use something else; I’m just using it for this example.

Anyways, I would suggest coding this part first and test it to get it working properly before adding an upwards angle.

Once you think you’re ready to add it, you can either simply add to the BodyVelocity (since you want to move it upwards, you would want to add to the Y value), or you can use HumanoidRootPart.CFrame.UpVector instead, which will change the velocity upwards in relation to the character instead of the world space.

If any of that is confusing, I can clarify more for you, but try to get the basic knockback working first.

1 Like

Another option if you don’t want to use a BodyVelocity object is to use the ApplyImpulse function on their HumanoidRootPart. From the entry on the wiki, “Impulses are useful for cases where you want a force applied instantly, such as an explosion or collision.” You can see here for more information.