I’m trying to make a knockback script but the knockback just moves the enemy the wrong way.
Script:
tool = script.Parent
handle = tool.Handle
enabled = true
local hits = {}
local function attack()
if enabled == false
then return end
enabled = false
handle.swing:Play()
handle.Touched:Connect(function(hit)
local hum = hit.Parent:FindFirstChildWhichIsA("Humanoid")
local char = hit.Parent
local root = char:FindFirstChild("HumanoidRootPart")
local otherroot = script.Parent.Parent:FindFirstChild("HumanoidRootPart")
if hum ~= nil and not hits[char] and root ~= nil and enabled == false then
hits[char] = true
hum:TakeDamage(50)
local lookvector = otherroot.CFrame.LookVector
task.wait()
local velo = Instance.new("BodyVelocity")
velo.Parent = root
velo.MaxForce = Vector3.new(9999,9999,9999)
velo.Velocity = lookvector * 1000
handle.stab:Play()
if hum.Health <= 0 then
task.wait(0.25)
handle.ban:Play()
end
task.spawn(function()
task.wait(2)
hits[char] = false
end)
task.wait(0.25)
velo:Destroy()
end
end)
task.wait(2)
enabled = true
end
tool.Activated:Connect(attack)
Additional Image
It just for some reason moves the dummy to the left.
Have you tried changing before the properties of the BodyVelocity then parenting it to the enemy root part?
Also, everytime you attack, you are creating a new .Touched function, which will eventually accumulate so much that everytime the ban hammer hits your game will lag to 1 frame per second. You must have the function separetely from the attack function, it should work fine since you added a enabled variable.