Changing Character's Position with RunService, Distance is Getting Longer

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

if enemyHrp.Position.Y <= 3.2 then connection:Disconnect() end

Try removing these if statements, and just have them a connection:Disconnect(). Then move the enemyHrp.Position.Y <= 3.2 to the first if statement at start of the connection.

It might be being added up because old connections are still running even after you’ve applied the force, and the script still thinks you’re attacking with the same knockbackType (function parameters won’t change even if the original value is, unless its a referenced object)

that wont work because opponents starts at Y position 3 when standing on the ground. I’ll try to separate the connections