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

how can i add knockback to this attack script?

hi, i made a script that damages a player or npc when they touch a brick but i need it to push them 3 studs backwards when they get hit.

this is the script (line 5 is what i thought could be a start. blade is the name of the part that damages the player.)

function onTouched(part)
 local h = part.Parent:findFirstChild("Humanoid")
 if h~=nil then
  h.Health = h.Health -5
  Blade:connect("Humanoid").FindPosition
  wait(2.00)
 end
end

script.Parent.Touched:connect(onTouched)

thanks in advance, from poopypigeon245

1 answer

You can use BodyForce or BodyVelocity. I made a debounce so it doesn't send you flying across the whole map in seconds.

local debounce = true
function onTouched(part)
 local h = part.Parent:findFirstChild("Humanoid")
 if h~=nil and debounce then --if humanoid is there, and if the debounce is true
  debounce = false --sets the debounce to false so it doesn't run like, 50000 times.
  h.Health = h.Health - 5
  spawn(function(sep) --to allow the script to run past the 0.1 without wating, only does it inside that function.
    local bodyVelocity = Instance.new("BodyVelocity", h.Parent)
    bodyVelocity.Velocity = Vector3.new(xvalue,yvalue,zvalue)
    wait(0.1) --change
    bodyVelocity:remove()
   end)
  Blade:connect("Humanoid").FindPosition-- (sorry, i don't know what this is for .n.)
  wait(2.00)
  debounce = true --sets debounce to tru
 end
end

script.Parent.Touched:connect(onTouched)

I might've messed up somewhere, sorry! If I did, please tell me where (if you get an error)

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