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
:
01 | local hum = script.Parent.Parent.Humanoid |
02 | local banditpunch 1 = hum:LoadAnimation(script.BPunch 1 ) |
03 | local human |
04 | local debounce = false |
05 | part = script.Parent |
06 |
07 | script.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 | banditpunch 1 :Play() |
13 | wait(. 12 ) |
14 | human.Health = human.Health - 5 |
15 | end |
16 | wait( 1 ) |
17 | debounce = false |
18 | banditpunch 1 :Stop() |
19 | end |
20 | end ) |
More or less like this
01 | --By Zgoly -- |
02 | --Variables-- |
03 | local Npc = script.Parent.Torso |
04 |
05 | --Code-- |
06 | local 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 = Vector 3. new( math.huge , math.huge , math.huge ) |
13 | bv.Velocity = Player.HumanoidRootPart.CFrame.lookVector *- 100 |
14 | wait( 0.1 ) |
15 | bv:remove() |
16 | end |
17 | end |
18 |
19 | Npc.Touched:connect(PlayerTouched) |
I suppose by adding knockback you mean pushing the target. To push a part, use the Velocity property:
01 | local hum = script.Parent.Parent.Humanoid |
02 | local banditpunch 1 = hum:LoadAnimation(script.BPunch 1 ) |
03 | local human |
04 | local debounce = false |
05 | part = script.Parent |
06 |
07 | script.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 | banditpunch 1 :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 |