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

How do I add knockback to this enemy NPCs damage script? [SOLVED]

Asked by 4 years ago
Edited 4 years ago

Hi. I'd like to add knockback to this NPCs damage script. Unfortunately, I'm not that experienced with Roblox Studio. Could someone please give me an example of how this can be done? Thanks!

Here's the NPCs script:

local hum = script.Parent.Parent.Humanoid
local banditpunch1 = hum:LoadAnimation(script.BPunch1)
local human
local debounce = false
part = script.Parent

script.Parent.Touched:connect(function(hit)
    if debounce == false then
        debounce = true
        human = hit.Parent:FindFirstChild("Humanoid")
        if (human ~= nil) then
            banditpunch1:Play()
            wait(.12)
            human.Health = human.Health - 5
        end
        wait(1)
        debounce = false
        banditpunch1:Stop()
    end
end)

2 answers

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

More or less like this

--By Zgoly --
--Variables--
local Npc = script.Parent.Torso

--Code--
local function PlayerTouched(Part)
    local Player = Part.Parent
        if game.Players:GetPlayerFromCharacter(Player) then
            Player.Humanoid.Health = Player.Humanoid.Health -5
            local bv = Instance.new("BodyVelocity")
            bv.Parent = Player.HumanoidRootPart
            bv.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
            bv.Velocity = Player.HumanoidRootPart.CFrame.lookVector *-100
            wait(0.1)
        bv:remove()
    end
end

Npc.Touched:connect(PlayerTouched)
0
WOW! THANKS! After like about 4 hours FINALLY! I changed it a bit but it works. SamuelaNutella 14 — 4y
Ad
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

I suppose by adding knockback you mean pushing the target. To push a part, use the Velocity property:

local hum = script.Parent.Parent.Humanoid
local banditpunch1 = hum:LoadAnimation(script.BPunch1)
local human
local debounce = false
part = script.Parent

script.Parent.Touched:connect(function(hit)
    if debounce == false then
        debounce = true
        human = hit.Parent:FindFirstChild("Humanoid")
        if (human ~= nil) then
            banditpunch1:Play()
            wait(.12)
            human.Health = human.Health - 5
         --Pushes the Hit part in the same direction as Part. "100" is the amount of force
         local direction = part.CFrame.lookVector * 100
            hit.Velocity = direction
        end
        wait(1)
        debounce = false
        banditpunch1:Stop()
    end
end)

Answer this question