If a player goes too high to quickly, my fall damage script breaks

Hello, I recently made a fall damage script and I now have a problem where if a player gains height quickly then loses a fraction of that height, the script will break and the fall damage will no longer work.

If you’re still confused, if a player repeatedly jumps on a high sloped plane like a ramp, the script will think the player will start to lose altitude, therefore it will break, though I do not know on how to tackle with this problem. Any help is appreciated!

connection = RunService.Stepped:Connect(function(delta)
	task.wait()
	local newPos = HRP.Position.Y
			
	if freeFallPos > newPos then
		print("Disconnecting")
		connection:Disconnect()
	end
	freeFallPos = newPos
end)

If I do not respond, I am simply sleeping

I don’t understand why you Disconnect the function when the criteria is matched.
I approached it by checking when the player landed and see what their Velocity was at that instant:

humanoid.StateChanged:Connect(function(oldState, newState)
	if newState == Enum.HumanoidStateType.Landed then
		speed = HRP.AssemblyLinearVelocity.Y
		print(speed)
	end
end)

That will show you how fast the player was descending at. It works OK for me so far

I disconnect the function so it can get passed through the other if statement.

if new == Enum.HumanoidStateType.Freefall then
		freeFallPos = HRP.Position.Y;
		velocity = HRP.AssemblyLinearVelocity.Y;
		print(velocity)
		
		local TimeNow = tick()
		connection = RunService.Stepped:Connect(function(delta)
			task.wait()
			local newPos = HRP.Position.Y
			
			if freeFallPos > newPos then
				print("Disconnecting")
				connection:Disconnect()
			end
			freeFallPos = newPos
		end)
		
		fallStart = tick()

	elseif new ~= Enum.HumanoidStateType.Landed then
		local fallStop = tick()
		local fallTime = fallStart - fallStop
		
		local newPos = HRP.Position.Y
		local magnitudePos = (freeFallPos - newPos)
		print(magnitudePos)
		print(velocity)
		if magnitudePos <= 0 then
			magnitudePos = nil
			return
		end
		
		if magnitudePos >= 20 then
			takeDamage = true
			if takeDamage == true then
				Hum:TakeDamage(math.abs((magnitudePos - fallTime) - (.25 * velocity)))

If this helps I’m showing the main part of the script instead of just the one few lines.

what part of this breaks? You shouldn’t use task.wait in the runservice connection, that might be messing up your values. .Stepped already runs once per physics frame so adding task.wait adds about or more than frame of lag before the next physics step; if you’re trying to get a once-per-frame after the physics step use Heartbeat.

Check the docs on task scheduling for more information on the order of a frame so you may avoid yeilding in those important connections.

Oh shoot I forgot to get rid of that