Using LinearVelocity to create a knockback

I am needing a knockback system in my game, I tried to use LinearVelocity but it doesnt work.

if knockback == true then
	local Attachment = Instance.new("Attachment", HitHumanoidRootPart)
						
	local Knockback = Instance.new("LinearVelocity", Attachment)
	Knockback.MaxForce = math.huge
	Knockback.VectorVelocity = HitHumanoidRootPart.CFrame.lookVector * 100
	Knockback.Attachment0 = Attachment
						
	game.Debris:AddItem(Knockback, 1)
end

I don’t know what I am doing wrong, as I find LinearVelocity hard to work with.

1 Like

In addition to that…

  • Make sure the Parts are Unanchored
  • Making the lookVector negative will move the player/object move backwards. I assume that’s the goal.

I figured out this issue today actually, here’s the code:

local function flyPlayerBackwards(player)
	local lookVector = player.Character.HumanoidRootPart.CFrame.LookVector 
	local pushBack = Instance.new("LinearVelocity")
	local finalVector = (-lookVector * 50 ) + Vector3.new(0,12,0)
	pushBack.VectorVelocity = finalVector
	pushBack.ForceLimitsEnabled = true
	pushBack.MaxForce = math.huge
	pushBack.Parent = player.Character.HumanoidRootPart
	pushBack.Attachment0 = player.Character.HumanoidRootPart.Attachment0

	pushBack.Enabled = true
	task.wait(0.2)
	pushBack:Destroy()				
end

I’ve found having the attachment already inside the player/object makes the process a lot easier. I think that could be the problem here. Sometimes the Active property of the LinearVelocity doesn’t toggle but the Enabled property does. I’m not entirely sure why it worked compared to before, but I hope this helps.

2 Likes