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)
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
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)
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)
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
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)