How to add knocback to a punch

i made a punch from. a youtube tutorial, it deals damage but no knockback idk how to put knockback i can send scripts if someone asks

check for humanoid root part and then add velocity to the humanoid root part.

something like this:

if humanoidRootPart then
	local knockback = Instance.new("LinearVelocity")

	knockback.Attachment0 = humanoidRootPart:FindFirstChild("RootAttachment")
	knockback.MaxForce = Vector3.new(1000, 0, 1000)
	knockback.VectorVelocity = humanoidRootPart.CFrame.LookVector * -50

	knockback.Parent = humanoidRootPart

	-- Apply the knockback for a short duration
	game:GetService("Debris"):AddItem(knockback, 0.5)
end

This probably won’t work but I hope it gives you a basic idea :slight_smile:

3 Likes

i’ve personally moved the logic to a function so i can reuse it somewhere else

function ImpulseAttachment(att: Attachment, velocity: Vector3, duration: number?)
	if tonumber(duration) == nil then
		duration = 1/3
	end
	
	local inst = Instance.new("LinearVelocity")
	inst.Attachment0 = att
	inst.MaxForce = math.huge
	inst.VectorVelocity = velocity / 2^duration
	inst.ForceLimitsEnabled = true
	inst.Parent = att.Parent
	
	game:GetService("Debris"):AddItem(inst, duration)
	return inst
end

local attachment: Attachment -- for example basically
ImpulseAttachment(attachment, Vector3.new(0,1/3,-1).Unit * 600)

but you need to use LinearVelocity for this, yeah

Or you can just do:

HumanoidRootPart:ApplyImpulse(Vector3.new(10, 10, 10)

As far as I know this would only run in a client sided script though since ApplyImpulse only works from the instances networkowner.

so far none of those work probably cuz im putting it in the wrong space but here is the server script, let me know if u need the local script ( u probably dont need that becuz its just hitbox and animation based )

server script:

local rs = game:GetService("ReplicatedStorage")

local events = rs:WaitForChild("Events")
local hitboxEvent = events:WaitForChild("Hitbox")



function newHitbox(character, size, offset, damage, linger)
	local hrp = character:FindFirstChild("HumanoidRootPart")
	if hrp == nil then return end
	local weld = Instance.new("WeldConstraint", hrp)
	local hitbox = Instance.new("Part")
	weld.Part0 = hrp
	weld.Part1 = hitbox

	hitbox.Transparency = 1
	hitbox.CanCollide = false
	hitbox.CanQuery = false
	hitbox.Massless = true

	hitbox.Size = size
	hitbox.CFrame = hrp.CFrame + hrp.CFrame.LookVector * offset.X + Vector3.new(0,offset.Y)
	hitbox.Parent = character

	hitbox.Touched:Connect(function(hit)
		if hit.Parent:FindFirstChild("Humanoid") == nil then return end

		for _, v in pairs(hitbox:GetChildren()) do
			if v:IsA("ObjectValue") then
				if v.Value == hit.Parent then return end
			end
		end

		local hitCounter = Instance.new("ObjectValue", hitbox)
		hitCounter.Value = hit.Parent

		hit.Parent.Humanoid:TakeDamage(damage)
		
		local sound = script.Sound:Clone()
		sound.Parent = hit.Parent:FindFirstChild("Humanoid")
		sound:Play()
		game.Debris:AddItem(sound,2)
		local punchParticle = game.ReplicatedStorage["Particle"].Attachment:Clone()
		punchParticle.Parent = hit.Parent:FindFirstChild("HumanoidRootPart")
		for i, v in pairs(punchParticle:GetChildren()) do
			if v:IsA("ParticleEmitter") then
				v:Emit(30)
				game.Debris:AddItem(punchParticle,0.4)
			end
		end
	end)

	game.Debris:AddItem(hitbox, linger)
end

hitboxEvent.OnServerEvent:Connect(function(plr, size, offset, damage, linger)
	newHitbox(plr.Character, size, offset, damage, linger)
end)

did you put anything for the parameters on the second to last line?

This is a bounce back script. Designed to do just that when it’s touched. This works well but, you will need to modify it a bit for your case. Everything you need is here and tested.

--serverscript .. atm in a part (with touch enabled) on the workspace

local db=true
script.Parent.Touched:Connect(function(hit)
	local humanoid=hit.Parent:FindFirstChildOfClass("Humanoid")
	if humanoid and db then	db=false
		local bv=Instance.new("BodyVelocity")
		bv.Parent=hit.Parent:WaitForChild("LowerTorso")
		bv.MaxForce=Vector3.new(100000, 100000, 100000)
		bv.Velocity=hit.Parent.Head.CFrame.LookVector * -80 
			+ hit.Parent.Head.CFrame.UpVector * 80
		wait(0.01)bv:Destroy()db=true
		wait(1)
	end
end)