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