I’m trying to create knockback for an attack, but I face an issue of delay. There doesn’t seem to be delay on damage taken though, so I don’t know how I could get rid of this for the knockback.
My code:
local char = plr.Character
local hitbox = game.ServerStorage.Hitbox:Clone()
local weld = Instance.new("WeldConstraint")
weld.Parent = game.Workspace
weld.Part0 = char.RightHand
weld.Part1 = hitbox
hitbox.CFrame = char.RightHand.CFrame
hitbox.Parent = workspace
local Speed = 40
local Force = 80000
local TotalForce = Force
local KnockBack = Instance.new("BodyVelocity")
local chars = {}
for i = 0,7 do
for _, part in pairs( workspace:GetPartsInPart(hitbox)) do
local echar = part.Parent
local hum = echar:FindFirstChild("Humanoid")
if table.find(chars,echar) == nil and echar ~= char then
if hum ~= nil then
table.insert(chars,echar)
KnockBack.Parent = echar.Parent:FindFirstChild("HumanoidRootPart")
KnockBack.MaxForce = Vector3.new(TotalForce,TotalForce,TotalForce)
KnockBack.Velocity = char:FindFirstChild("HumanoidRootPart").CFrame.LookVector * Speed
hum.Health -= 10
end
end
end
task.wait(0.02)
end
hitbox:Destroy()
end)
The module script that is being called:
function kb.Start(character, direction)
print("ran3")
local hrp = character:FindFirstChild("HumanoidRootPart")
if hrp == nil then return end
local att = Instance.new("Attachment", hrp)
local Lv =Instance.new("LinearVelocity", att)
Lv.MaxForce = 9999999999
Lv.VectorVelocity = direction
Lv.Attachment0 = att
game.Debris:AddItem(att,0.1)
end
return kb