ServerScript in character:
local runService = game:GetService("RunService")
local SSS = game:GetService("ServerScriptService")
local bindable = SSS.Bindables.Knockback
local SPEED = 65
local GRAVITY_MULT = 2.5
local gravity = Vector3.new(0, -workspace.Gravity, 0) * GRAVITY_MULT
local attacker
local attack
bindable.Event:Connect(function(char, atkChar, knockbackType)
if char == script.Parent and knockbackType == 1 or knockbackType == 2 or knockbackType == 3 then
attacker = atkChar
attack = knockbackType
local enemyHrp = char.HumanoidRootPart
local atkHrp = atkChar.HumanoidRootPart
local y = enemyHrp.Position.Y
local cf = CFrame.new(atkHrp.CFrame.Position + Vector3.new(0, y-7, 0))
local velocity = (enemyHrp.Position - cf.Position).Unit * SPEED
local position = atkHrp.Position
local totalDistance = 0
local connection
connection = runService.Heartbeat:Connect(function(deltaTime)
if attacker ~= atkChar or attack ~= knockbackType then connection:Disconnect() end
local stepVelocity = velocity + gravity * deltaTime
local stepDisplacement = (velocity + stepVelocity) / 2 * deltaTime
velocity = stepVelocity
local nextPosition = enemyHrp.Position + stepDisplacement
enemyHrp.CFrame = CFrame.new(nextPosition, atkHrp.Position)
position = nextPosition
totalDistance += stepDisplacement.Magnitude
if enemyHrp.Position.Y <= 3.2 then connection:Disconnect() end
print("flinch")
end)
elseif char == script.Parent and knockbackType == 4 then
attacker = atkChar
attack = knockbackType
local enemyHrp = char.HumanoidRootPart
local atkHrp = atkChar.HumanoidRootPart
local gravity2 = Vector3.new(0, -workspace.Gravity, 0)
local y = enemyHrp.Position.Y
local cf = CFrame.new(atkHrp.CFrame.Position + Vector3.new(0, y-7, 0))
local velocity = (enemyHrp.Position - cf.Position).Unit * SPEED
local position = atkHrp.Position
local totalDistance = 0
local connection
connection = runService.Heartbeat:Connect(function(deltaTime)
if attacker ~= atkChar or attack ~= knockbackType then connection:Disconnect() end
local stepVelocity = velocity + gravity2 * deltaTime
local stepDisplacement = (velocity + stepVelocity) / 2 * deltaTime
velocity = stepVelocity
local nextPosition = enemyHrp.Position + stepDisplacement
enemyHrp.CFrame = CFrame.new(nextPosition, atkHrp.Position)
position = nextPosition
totalDistance += stepDisplacement.Magnitude
if enemyHrp.Position.Y <= 3.2 then connection:Disconnect() end
print("send flying")
end)
end
end)
Result:
Why is the opponent going back further every hit? The script should make the second & third push back be the same distance as the first. And I’m not sure but the 4th hit might be doing flinch sometimes instead of the RunService for attack 4. Helppp im stuck T-T