Best knockback system?

Hello, I need help with making a knockback system. I have the lookVector (aka starting position and target position), the speed, the mass of the model, but I can’t find a way to make a good knockback system. Thins I have tried so far are:
BodyVelocity (deprecated),
ApplyImpulse() (sometimes doesn’t work because of different mass)
VectorForce (sometimes doesn’t work on lighter objects)
basePart.Velocity (I have to set it back to Vector3.new(0,0,0) after 0.1 seconds so it’s a bit unrealistic)
Any suggestions with code samples? Sometimes I don’t know if I should change/remove mass inside the formula which multiplies the lookvector by. There could have been a miscalculation in my code.

local result = (lookVector) * mass * speed
result = Vector3.new(result.X,mass*speed,result.Z)
arg.Part:ApplyImpulse(result)
local result = (lookVector) * mass * speed
result = Vector3.new(result.X,mass*speed,result.Z)
local vecForce = Instance.new("VectorForce",arg.Part)
vecForce.Force = result
task.wait(.1)
vecForce:Destroy()
7 Likes

Just a suggestion for the .Velocity workaround, you could have it in a loop to slowly decrease it?

local knockback = function(lookVector, mass, speed, arg, dec)
	local result = (lookVector) * mass * speed
	result = Vector3.new(result.X,mass*speed,result.Z)
	local vecForce = Instance.new("VectorForce",arg.Part)
	vecForce.Force = result
	repeat
		task.wait(.1)
		local newRes = Vector3.new((result.X-dec), (result.Y-dec), (result.Z-dec))
		result = newRes
		vecForce.Force = newRes
	until vecForce.Force == Vector3.new(0,0,0)
	vecForce:Destroy()
end
2 Likes

hey quick tip, do the kb on the client for smoother kb and use a velocity constraint like bodyvelocity

6 Likes

You definitely wanna do knockback client-side.

I’ve seen games that did it on the server, if you have a lag spike you go FLYING and sometimes fall off the map from even the most minor hits because the lag causes knockback to be turned off / decay too late.

Server side knockback can also completely fail to register, resulting in no knockback being applied at all if the player has high latency / slow internet.

2 Likes

I would suggest you tween a CFrame lerp of the Player, that way it can’t be cheated on the client and will work on the server without interruption from player lag, updates properly on all clients, etc.

1 Like

Good idea. But what if they receive two hits in a short time and the loops overwrite each other, causing weird behaviors? What ways do we have to solve this? If .velocity were a number value, I can easily just add it upon receiving knockback and subtract it inside the loop. Would this be possible with vector3?

1 Like

Some knockback are dealt to npcs though. What can I do to make them seen by everyone?

1 Like

Apply it on the server if the network ownership is owned by the server, otherwise you gotta deal the knockback on whoever’s client currently simulates the NPC’s physics.

1 Like

Well as the other guy suggested you could run it client side and have a server event update the knockback value and have the loop restart

1 Like

The main question is how would I apply the knockback?

1 Like

You can use a velocity constraint to apply a more constant force temporarily, or you can set the velocity property of the humanoid root part directly but honestly try out a few things and see which one works best.

https://create.roblox.com/docs/reference/engine/classes/LinearVelocity

Hey, I know I’m super late to the party, but in order for ApplyImpulse() to work on parts with mass, you can always make those parts massless, unless your game relies on physics heavily.

how would i do that? im terrible with network ownership and that kinda stuff

Players have network ownership over their own character, applying knockback on the client should have immediate effect.

Regardless of what method you end up using for your knockback system, there’s gonna be some jankyness that comes to replication, regardless of network ownership. You should consider what type of game you’re making, a fighting game for example would probably use server sided knockback with some method for adjusting for latency. If it is not a fighting game or any kind of competitive game, then I’d just handle the knockback on the client side like many other’s have mentioned, that way it’s much easier to implement. On another note, when I was facing similar issues in the past, I actually ended using TweenService to interpolate through a bezier curve.

1 Like

Use BodyVelocity. It is indeed deprecated, but that just means it won’t receive any more updates and has since been replaced. It is still good to use, though. I use it all the time, and so do big games like item asylum. just set the MaxVelocity to Vector3.new(math.huge, math.huge, math.huge), and set the Velocity to the LookVector times however much you want. Parent it to the HumanoidRootPart. Then do task.wait() and delete it.