Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I add push/knockback after the tool touches a Humanoid?

Asked by 5 years ago

I created a tool, that has a handle inside of it followed by a Damage script so that if the tool touches a Humanoid it deals 25 damage, but how would I make the humanoid get knocked backward a few steps if the tool touched it?

Damage Script:

local player = game.Players.LocalPlayer
local handle = script.Parent.Handle
local debounce = false 

handle.Touched:Connect(function(hit)
    if not debounce and hit and hit.Parent.Humanoid then
        debounce = true

        hit.Parent.Humanoid:TakeDamage(25)
        wait(15)

        debounce = false
    end
end)

=====================================================================================================================================================

0
Insert a BodyForce in the player's torso learn more on the wiki TiredMelon 405 — 5y
0
To get a direction vector from point A to point B, do `(B - A).unit * speed`. So if you wanted to fling a brick from (1, 2, 3) towards (4, 5, 6) with a speed of 5, you'd do `((4, 5, 6) - (1, 2, 3)).unit * 5`. fredfishy 833 — 5y
0
is the humanoid part of a player? SteamG00B 1633 — 5y

1 answer

Log in to vote
0
Answered by
SteamG00B 1633 Moderation Voter
5 years ago
Edited 5 years ago

If this is about causing knockback in a player, or an NPC you would clone from storage, something I like to do in this case is to have a bodyforce in the primarypart of the player or NPC before it gets put into the workspace. That way, you just access the NPC or player's bodyForce and apply a force to it whenever you want.

So it seems as if you are attacking NPCs, so to have a script place a bodyVelocity in an NPC the script would be like:

workspace.ChildAdded:Connect(function(child)
    if child.Name == "whatever the bodyforce is going to be in" then--this would be the model of the NPC
        local bv = Instance.new("BodyVelocity")
        bv.Parent = child.PrimaryPart
    end
end)

Your script would look like:

local player = game.Players.LocalPlayer
local handle = script.Parent.Handle
local debounce = false 
local playerPos = player.PrimaryPart.CFrame

handle.Touched:Connect(function(hit)
    if not debounce and hit and hit.Parent.Humanoid then
        debounce = true
    local hitPos = hit.Parent.PrimaryPart.CFrame
        hit.Parent.Humanoid:TakeDamage(25)
    hit.Parent.PrimaryPart.bodyForce.Force = (playerPos-hitPos).unit*-3 --negative so it goes away from you
    wait(.5)
    hit.Parent.PrimaryPart.bodyForce.Force = Vector3.new(0,0,0)
        wait(14.5)--keeps the original 15 seconds, while allowing the force to stop after .5 seconds

        debounce = false
    end
end)

To explain the bodyForce line, it makes it so that Point A is you and Point B is the enemy, but it is multiplied by a negative number so that the enemy is knocked away from you while being in line with you.

To visualize this, you are the end of a stick, the enemy is the fulcrum (the point a lever rotates on) and if we multiplied the force by a positive number, the enemy would go towards you, if we multiply it by a negative number, the enemy goes to the opposite end of the stick.

0
The target doesn't get knocked back nor does the tool do damage anymore. Incinerxte 2 — 5y
0
does it throw any errors? SteamG00B 1633 — 5y
0
Weird thing is there's no errors. Incinerxte 2 — 5y
0
So I updated my answer, so that the bodyVelocity placement wouldn't be causing any errors SteamG00B 1633 — 5y
View all comments (3 more)
0
Still nothing, no errors at all. Incinerxte 2 — 5y
0
Try adding prints to print the values of each variable before and after it changes SteamG00B 1633 — 5y
0
Found an error, "Players.Incinerxte.Backpack.Rasengan.Damage:4: attempt to index local 'player' (a nil value)" [Incinerxte is my username] Incinerxte 2 — 5y
Ad

Answer this question