How do i add knockback help me please im going insane

please help me add knockback to my code please
I’ved tried like bodyvelocity and linear velocity and stuff like that but I don’t get it so it just doesn’t work ughhhh

local plr = game.Players.LocalPlayer
local tool = script.Parent

local char = plr.Character
local hum = char:WaitForChild("Humanoid")


local combo = 0
local db = true

tool.Activated:Connect(function()
	if db == true then
		db = false
		
		combo = combo + 1

		local hb = Instance.new("Part",workspace)
		hb.Name = "HITBOX"
		hb.Size = Vector3.new(4, 2.5, 1.5)
		hb.CanCollide = false
		hb.Anchored = false
		hb.CFrame = char.HumanoidRootPart.CFrame:ToWorldSpace(CFrame.new(0, 0, -3))
		hb.Transparency = .5

		local w = Instance.new("WeldConstraint")
		w.Part0 = char.HumanoidRootPart
		w.Part1 = hb
		w.Parent = hb
		
		hb.Touched:Once(function(hit)
			if hit.Parent:FindFirstChild("Humanoid") and hit.Parent.Name ~= plr.Name then
				hit.Parent.Humanoid:TakeDamage(10)
			end
		end)


		print("Animation: "..combo)
		
		if combo == 1 then
			hum:LoadAnimation(script["1"]):Play()
		elseif combo == 2 then
			hum:LoadAnimation(script["2"]):Play()
		elseif combo == 3 then
			hum:LoadAnimation(script["3"]):Play()
		elseif combo == 4 then
			hum:LoadAnimation(script["4"]):Play()
		end
		game.Debris:AddItem(hb,.5)
		wait(.7)
		
		if combo > 3 then
			combo = 0
		end
		
		db = true
	end

end)

would appreciate help cuz I’m going insane

2 Likes

You could try setting the target’s Torso velocity to a unit value and multiply it by the speed you want, but this works more affectively on NPCS, and not Player

target.Torso.Velocity = (target.Torso.Position - player.Torso.Position).Unit * velocity

1 Like

where do i put that in the script?

1 Like

You have to put that into the touched function.

1 Like

it doesn’t work

local plr = game.Players.LocalPlayer
local tool = script.Parent

local char = plr.Character
local hum = char:WaitForChild("Humanoid")


local combo = 0
local db = true

tool.Activated:Connect(function()
	if db == true then
		db = false
		
		combo = combo + 1

		local hb = Instance.new("Part",workspace)
		hb.Name = "HITBOX"
		hb.Size = Vector3.new(4, 2.5, 1.5)
		hb.CanCollide = false
		hb.Anchored = false
		hb.CFrame = char.HumanoidRootPart.CFrame:ToWorldSpace(CFrame.new(0, 0, -3))
		hb.Transparency = .5

		local w = Instance.new("WeldConstraint")
		w.Part0 = char.HumanoidRootPart
		w.Part1 = hb
		w.Parent = hb
		
		hb.Touched:Once(function(hit)
			if hit.Parent:FindFirstChild("Humanoid") and hit.Parent.Name ~= plr.Name then
				hit.Parent.Humanoid:TakeDamage(10)
				script.hit:Play()
				hit.Parent.Torso.Velocity = (hit.Parent.Torso.Position - char.Torso.Position).Unit * 45
			end
		end)


		print("Animation: "..combo)
		
		if combo == 1 then
			hum:LoadAnimation(script["1"]):Play()
		elseif combo == 2 then
			hum:LoadAnimation(script["2"]):Play()
		elseif combo == 3 then
			hum:LoadAnimation(script["3"]):Play()
		elseif combo == 4 then
			hum:LoadAnimation(script["4"]):Play()
		end
		game.Debris:AddItem(hb,.5)
		wait(.7)
		
		if combo > 3 then
			combo = 0
		end
		
		db = true
	end

end)

1 Like

You can use BasePart:ApplyImpulse. Here’s what you’ll need:

  1. The direction you wish to knockback the assembly (a unit vector)
  2. The assembly’s mass
  3. Your desired speed for the knockback

Multiply the mass with the speed, then multiply the answer with the direction to get the impulse

The formula will need to be more complex if you want the knockback to reach a specific distance, though

1 Like

im stupid would you be so kind to make it for me??? pls

1 Like

This should work assuming that the character detected by the hitbox is the one that will be knocked back:

local IMPULSE_SPEED = 10000 -- The speed of the impulse (in studs per second)
local IMPULSE_DIRECTION = Vector3.zAxis -- The direction of the impulse (needs to be a unit vector)

local plr = game.Players.LocalPlayer
local tool = script.Parent

local char = plr.Character
local hum = char:WaitForChild("Humanoid")


local combo = 0
local db = true

tool.Activated:Connect(function()
	if db == true then
		db = false

		combo = combo + 1

		local hb = Instance.new("Part")
		hb.Name = "HITBOX"
		hb.Size = Vector3.new(4, 2.5, 1.5)
		hb.CanCollide = false
		hb.Anchored = false
		hb.CFrame = char.HumanoidRootPart.CFrame:ToWorldSpace(CFrame.new(0, 0, -3))
		hb.Transparency = .5
		hb.Parent = workspace

		local w = Instance.new("WeldConstraint")
		w.Part0 = char.HumanoidRootPart
		w.Part1 = hb
		w.Parent = hb

		hb.Touched:Once(function(hit)
			if hit.Parent:FindFirstChild("Humanoid") and hit.Parent.Name ~= plr.Name then
				hit.Parent.Humanoid:TakeDamage(10)

				if hit.Parent.PrimaryPart then
					local mass = hit.Parent.PrimaryPart.AssemblyMass

					hit.Parent.PrimaryPart:ApplyImpulse(IMPULSE_DIRECTION * (mass * IMPULSE_SPEED))
				end
			end
		end)


		print("Animation: "..combo)

		if combo == 1 then
			hum:LoadAnimation(script["1"]):Play()
		elseif combo == 2 then
			hum:LoadAnimation(script["2"]):Play()
		elseif combo == 3 then
			hum:LoadAnimation(script["3"]):Play()
		elseif combo == 4 then
			hum:LoadAnimation(script["4"]):Play()
		end
		game.Debris:AddItem(hb,.5)
		wait(.7)

		if combo > 3 then
			combo = 0
		end

		db = true
	end

end)
1 Like

1 Like

I forgot to mention that if the network owner of the NPCs is the server, the impulse will need to be applied on the server, sorry about that

Make sure to check that the player is actually within distance of the NPC before applying the impulse on the server, though, otherwise an exploiter would be able to push NPCs no matter where they are


@Crust224

Here’s the new LocalScript:

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local applyImpulseRE = ReplicatedStorage:WaitForChild("ApplyImpulseRE")

local plr = game.Players.LocalPlayer
local tool = script.Parent

local char = plr.Character
local hum = char:WaitForChild("Humanoid")


local combo = 0
local db = true

tool.Activated:Connect(function()
	if db == true then
		db = false

		combo = combo + 1

		local hb = Instance.new("Part",workspace)
		hb.Name = "HITBOX"
		hb.Size = Vector3.new(4, 2.5, 1.5)
		hb.CanCollide = false
		hb.Anchored = false
		hb.CFrame = char.HumanoidRootPart.CFrame:ToWorldSpace(CFrame.new(0, 0, -3))
		hb.Transparency = .5

		local w = Instance.new("WeldConstraint")
		w.Part0 = char.HumanoidRootPart
		w.Part1 = hb
		w.Parent = hb

		hb.Touched:Once(function(hit)
			if hit.Parent:FindFirstChild("Humanoid") and hit.Parent.Name ~= plr.Name then
				hit.Parent.Humanoid:TakeDamage(10)
				applyImpulseRE:FireServer(hit.Parent)
			end
		end)


		print("Animation: "..combo)

		if combo == 1 then
			hum:LoadAnimation(script["1"]):Play()
		elseif combo == 2 then
			hum:LoadAnimation(script["2"]):Play()
		elseif combo == 3 then
			hum:LoadAnimation(script["3"]):Play()
		elseif combo == 4 then
			hum:LoadAnimation(script["4"]):Play()
		end
		game.Debris:AddItem(hb,.5)
		wait(.7)

		if combo > 3 then
			combo = 0
		end

		db = true
	end

end)

And here’s the server Script:

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local IMPULSE_SPEED = 10000 -- The speed of the impulse (in studs per second)
local IMPULSE_DIRECTION = Vector3.zAxis -- The direction of the impulse (needs to be a unit vector)
local DISTANCE_FROM_NPC = 16 -- Maximum range for the player to be able to push the NPC (in studs)

local applyImpulseRE = ReplicatedStorage:WaitForChild("ApplyImpulseRE")


local function onServerEvent(player: Player, npc: Model)
	if
		player.Character
		and typeof(npc) == "Instance"
		and npc:IsA("Model")
		and npc.PrimaryPart
	then
		if player:DistanceFromCharacter(npc.PrimaryPart.Position) <= DISTANCE_FROM_NPC then
			local mass = npc.PrimaryPart.AssemblyMass

			npc.PrimaryPart:ApplyImpulse(IMPULSE_DIRECTION * (mass * IMPULSE_SPEED))
		end
	end
end

applyImpulseRE.OnServerEvent:Connect(onServerEvent)

You’ll need to add a RemoteEvent named “ApplyImpulseRE” as a child of ReplicatedStorage for the scripts to work

LinearVelocity works the best for me. You just need to make sure you set the max velocity and give it an attachment0. By default R6 characters have a RootAttachment and R15 characters have a RootRigAttachment. (And you don’t need to factor in mass)

1 Like

Thanks a lot for the help but I think I figured it out!!!

1 Like

thank youuuuuuuuuuuuuuuuuuuuuuu

1 Like

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