How to make knockback for my combat system?

hello. I’ve been trying to make a knockback system for the past few hours, and i’ve looked around on the devforum. none of it seemed to work. i have a function inside of a module script in serverscriptservice that i can easily recall to when i want to knock a player back.
here it is:

local knockback = {}
	local function knockback(force, hitcharacter, caster)
		local hum = hitcharacter:FindFirstChild("Humanoid")
		hum.PlatformStand = true
		
		local direction = (caster.HumanoidRootPart.Position - hitcharacter.HumanoidRootPart.Position).Unit
		
		hitcharacter.HumanoidRootPart.AssemblyLinearVelocity = Vector3.new(0, force * 0.5, direction * -50)
		
		task.wait(0.2)
		hum.PlatformStand = false
	end
return knockback

please help me

Hello. A better and easier way to do this is using BodyForce. It is deprecated, but It stills work. I see a lot of developers using it. (Note: BodyForce is deprecated, so it won’t get updates anymore)

This is what i have from a recent project. If you put it in a part you can adjust things.


local function applyKnockback(target, direction, force)

	local humanoid = target.Parent:FindFirstChildOfClass("Humanoid")

	if humanoid then
		humanoid:ChangeState(Enum.HumanoidStateType.Ragdoll)
		humanoid.AutoRotate = false

		local dirPart = Instance.new("Part")
		dirPart.Transparency = 1
		dirPart.Position = target.Position - (-direction * (force/3))
		dirPart.Anchored = true
		dirPart.CanCollide = false
		dirPart.Parent = target

		local attachment1 = Instance.new("Attachment", target)
		local attachment2 = Instance.new("Attachment", dirPart)

		local linearVelocity= Instance.new("LinearVelocity")
		linearVelocity.Attachment0 = attachment1
		linearVelocity.Attachment1 = attachment2
		linearVelocity.Visible = true
		linearVelocity.RelativeTo = Enum.ActuatorRelativeTo.Attachment1
		linearVelocity.VectorVelocity = direction * force

		linearVelocity.MaxForce = 10000
		linearVelocity.Parent = target

		task.wait(.5)
		humanoid.AutoRotate = true
		linearVelocity:Destroy()
	end
end

local canTouch = true
local function onHit(target)
	if canTouch then
		canTouch = false	
		if target:FindFirstChild("Handle") or target.Name == "Handle" then
		else
			local character = script.Parent
			if target.Parent.Humanoid then
				local knockbackDirection = (target.Position - character.Position).Unit * Vector3.new(1,3,1)/2
				local knockbackForce = 50
				applyKnockback(target, knockbackDirection, knockbackForce)
			end
		end
		task.wait(.2)
		canTouch = true
	end
end

script.Parent.Touched:Connect(onHit)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.