How should I approach making a knockback system?

Good day, everyone

I would like to make a knockback system where a player gets launched back while playing an animation. How should I do this? I’ve never made anything like this before and I really need help. I’m not asking for a script, I am just asking how I should execute this.

Thank you.

2 Likes

I think changing the Velocity should give you a great result.

2 Likes

I made this script;

local part = script.Parent

function onTouch(hit)
	
	if hit.Parent == nil then return end
	local h = hit.Parent:FindFirstChild("Humanoid")
	if h ~= nil then
		
		print"player"
		
		local player = game:GetService("Players")[hit.Parent.Name]
		local bv = Instance.new("BodyVelocity", player)
		local hasBV = false
		
		if hasBV == false then
			bv.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
			bv.P = 1000
			bv.Velocity = Vector3.new(5, 0, 0)
			hasBV = true
		end
	end
end

part.Touched:Connect(onTouch)

It doesn’t seem to work though. Did I use BodyVelocity in a wrong way? I’ve never used it before.

1 Like

Okay, I changed some stuff. It works. But how can I make a character play an animation while getting pushed back?

2 Likes

Try Using This Maybe:

function onTouch(hit)
	
	if hit.Parent == nil then return end
	local h = hit.Parent:FindFirstChild("Humanoid")
	if h ~= nil then
		
		print"player"
		
		local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent) -- Here Is the problem maybe
		local bv = Instance.new("BodyVelocity", player)
		local hasBV = false
		
		if hasBV == false then
			bv.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
			bv.P = 1000
			bv.Velocity = Vector3.new(5, 0, 0)
			hasBV = true
		end
	end
end

part.Touched:Connect(onTouch)
1 Like

https://developer.roblox.com/en-us/api-reference/class/Animation

and Devking Video:

2 Likes

Now, that I’ve gotten the BodyVelocity to work, how do you get the character to stop flying back?

local part = script.Parent

function onTouch(hit)
	
	if hit.Parent == nil then return end
	local h = hit.Parent:FindFirstChild("Humanoid")
	if h ~= nil then
		
		print"player"
		
		local player = hit.Parent.HumanoidRootPart

		if not hit.Parent.HumanoidRootPart:FindFirstChild("BodyVelocity") then
			
			local bv = Instance.new("BodyVelocity", player)
			bv.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
			bv.P = 1000
			bv.Velocity = Vector3.new(-100, 0, 0)

		end
	end
end

part.Touched:Connect(onTouch)

Oh, and what does the “P” property do?

1 Like

Let me help

local part = script.Parent

function onTouch(hit)
	
	if hit.Parent == nil then return end
	local h = hit.Parent:FindFirstChild("Humanoid")
	if h ~= nil then
		
		print"player"
		
		local player = hit.Parent.HumanoidRootPart

		if not hit.Parent.HumanoidRootPart:FindFirstChild("BodyVelocity") then
			
			local bv = Instance.new("BodyVelocity", player)
			bv.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
			bv.P = 1000
			bv.Velocity = Vector3.new(-100, 0, 0)

		end
        wait(0.25)
        bv:Destroy()
	end
end

part.Touched:Connect(onTouch)
2 Likes

I think it’s can help you

2 Likes

Setting the BodyVelocity’s Velocity to Vector3.new() (0,0,0) should stop the player, and don’t forget to destroy the BodyVelocity afterwards.*

*It should also be noted that only the first BodyVelocity instance put inside a part will apply velocity to the part.

1 Like

You can make it using linear velocity and an attachment. I think you can do it with something like this:
local part = script.Parent

function KnockBackOnHit(hit)
if hit.Parent == nil then return end

local h = hit.Parent:FindFirstChild("Humanoid")
local hrp = hit.Parent:FindFirstChild("HumanoidRootPart")

if h ~= nil then
	local att = Instance.new("Attachment", hrp)
	local kbvel = Instance.new("LinearVelocity", att)
	local hasLV = false

	if hasLV == false then
		kbvel.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
		kbvel.VelocityConstraintMode = Enum.VelocityConstraintMode.Vector
		kbvel.Attachment0 = att
		kbvel.VectorVelocity = hrp.Position - part.Position * Vector3.new(5, 0, 0)
		game.Debris:AddItem(att, 0.4)
	end
end

end
part.Touched:Connect(KnockBackOnHit)

Sorry if it formats wrongly.

If it were for a player, it’d be that player’s humanoidrootpart position minus your humanoidrootpart position. It was just a simple way to do knockback

1 Like