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 5 years ago
Edited 5 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:

01local hum = script.Parent.Parent.Humanoid
02local banditpunch1 = hum:LoadAnimation(script.BPunch1)
03local human
04local debounce = false
05part = script.Parent
06 
07script.Parent.Touched:connect(function(hit)
08    if debounce == false then
09        debounce = true
10        human = hit.Parent:FindFirstChild("Humanoid")
11        if (human ~= nil) then
12            banditpunch1:Play()
13            wait(.12)
14            human.Health = human.Health - 5
15        end
16        wait(1)
17        debounce = false
18        banditpunch1:Stop()
19    end
20end)

2 answers

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

More or less like this

01--By Zgoly --
02--Variables--
03local Npc = script.Parent.Torso
04 
05--Code--
06local function PlayerTouched(Part)
07    local Player = Part.Parent
08        if game.Players:GetPlayerFromCharacter(Player) then
09            Player.Humanoid.Health = Player.Humanoid.Health -5
10            local bv = Instance.new("BodyVelocity")
11            bv.Parent = Player.HumanoidRootPart
12            bv.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
13            bv.Velocity = Player.HumanoidRootPart.CFrame.lookVector *-100
14            wait(0.1)
15        bv:remove()
16    end
17end
18 
19Npc.Touched:connect(PlayerTouched)
0
WOW! THANKS! After like about 4 hours FINALLY! I changed it a bit but it works. SamuelaNutella 14 — 5y
Ad
Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

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

01local hum = script.Parent.Parent.Humanoid
02local banditpunch1 = hum:LoadAnimation(script.BPunch1)
03local human
04local debounce = false
05part = script.Parent
06 
07script.Parent.Touched:connect(function(hit)
08    if debounce == false then
09        debounce = true
10        human = hit.Parent:FindFirstChild("Humanoid")
11        if (human ~= nil) then
12            banditpunch1:Play()
13            wait(.12)
14            human.Health = human.Health - 5
15         --Pushes the Hit part in the same direction as Part. "100" is the amount of force
View all 23 lines...

Answer this question