Laggy Knockback

I have a knockback system that works very well with players because I do the knockback on the client, but since NPCs don’t have clients the knockback is done on the server. Here is the code:

if Player ~= nil then

						local velocity = ((rootPart.Position - Values.PlayerWhoHit.HumanoidRootPart.Position).Unit * Values.KnockbackPower) + Vector3.new(0,Values.UpKnockback,0)
						KnockbackEvent:FireClient(Player,velocity)

					else

						local Attachment = Instance.new("Attachment")
						Attachment.Parent = rootPart
						local LinearVelocity = Instance.new("LinearVelocity")
						LinearVelocity.Parent = rootPart

						LinearVelocity.MaxForce = 100000
						LinearVelocity.VectorVelocity = ((rootPart.Position - Values.PlayerWhoHit.HumanoidRootPart.Position).Unit * Values.KnockbackPower) + Vector3.new(0,Values.UpKnockback,0)
						LinearVelocity.Attachment0 = Attachment
						Values.KnockbackPower = 0
						Values.UpKnockback = 0
						DebrisService:AddItem(Attachment,0.1)
						DebrisService:AddItem(LinearVelocity,0.1)

					end

There is normally a small delay when the NPC gets hit before the knockback is applied which in most cases is not an issue, but when there are a lot of attacks all hitting in quick succession, this script fails to apply the knockback properly and the NPC will stay still and then all of the knockback is applied once the attacks stop. So in other words the NPC can get hit by 10 attacks that do 1 stud of knockback quickly and not get knocked back, but when the attacks stop, the knockback is all applied at once and they go flying back 10 studs all at once.

The problem is that you don’t interrupt the linearvelocity. Instead of spawning a new one everytime knockback is done, you should have a single one that’s used for being pushed around.

2 Likes

If I were you I would try tweening the vector velocity.