Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ Accepted answer

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)

1 comment

0

WOW! THANKS! After like about 4 hours FINALLY! I changed it a bit but it works.

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)

This discussion is preserved as it was. New answers, comments, and votes are closed.