Weird issue with vaulting mechanic

im having this weird error with my vaulting script, when on the ground it works, it creates a bodyvelocity and pushes the player up and forward but in the air the player just falls, also the body velocity doesnt work every time

videos explaining: robloxapp-20230220-1048444.wmv (5.0 MB)

its a local script and it goes in startercharacterscripts

local torso = game.Players.LocalPlayer.Character:WaitForChild("Torso")

local char = game.Players.LocalPlayer.Character

local hrp = char:WaitForChild("HumanoidRootPart")

local uis = game:GetService("UserInputService")

local hum = char:WaitForChild("Humanoid")

local idle = script:WaitForChild("Idle")
local idleAnim = hum:WaitForChild("Animator"):LoadAnimation(idle)

local Move = script:WaitForChild("Move")
local MoveAnim = hum:WaitForChild("Animator"):LoadAnimation(Move)




local db = false


local vaulting = Instance.new("BoolValue")
vaulting.Parent = game.Players.LocalPlayer.Character
vaulting.Name = "VaultingVal"

local newOrientation = hrp.Orientation

while wait() do
	local lv = char.Torso.CFrame.LookVector
	local origin = char.Torso.Position
	local direction = lv * 100
	local raycastResult = workspace:Raycast(origin,direction)
	if raycastResult then
			if db == false then
				uis.InputBegan:Connect(function(inp1) 
				if inp1.KeyCode == Enum.KeyCode.Space or hum.FloorMaterial == Enum.Material.Air then
					if raycastResult.Instance.Name == "Ledge" then	
						if (torso.Position - raycastResult.Position).Magnitude < 3 and (torso.Position - raycastResult.Position).Magnitude > 0.5 then	
							hrp.Anchored = true
							idleAnim:Play()
							MoveAnim:Stop()
							vaulting.Value = true
							    end
							end
						end
					end)
				uis.InputBegan:Connect(function(inp)
				if vaulting.Value == true then	
					if inp.KeyCode == Enum.KeyCode.W or game.ReplicatedStorage.Sprinting.Value == true then
						vaulting.Value = false	
						hrp.Anchored = false
						idleAnim:Stop()
						MoveAnim:Play()
					local BodyVelocity = Instance.new("BodyVelocity")
							BodyVelocity.Parent = hrp
					
					local originalOrientation = hrp.Orientation

					if newOrientation ~= originalOrientation then
								originalOrientation = newOrientation
								hrp.Anchored = false
							BodyVelocity.MaxForce = Vector3.new(0, 1000, 0)
								hum.PlatformStand = false
								BodyVelocity.Velocity = hrp.CFrame.LookVector * 60
								wait(0.1)
								hum.PlatformStand = true
								BodyVelocity.MaxForce = Vector3.new(5000, -75, 5000)
								BodyVelocity.Velocity = hrp.CFrame.LookVector * 60
					end
						wait(0.1)
						hum.PlatformStand = false
						BodyVelocity:Destroy()
						db = true
						wait(1)
						db = false
					end	
				end
				
			if inp.KeyCode == Enum.KeyCode.A or inp.KeyCode == Enum.KeyCode.S or inp.KeyCode == Enum.KeyCode.D then
			if vaulting.Value == true then	
			idleAnim:Stop()
			MoveAnim:Stop()
			hrp.Anchored = false
			vaulting.Value = false
						local BodyVelocity2 = Instance.new("BodyVelocity")
						BodyVelocity2.Parent = hrp

						local originalOrientation2 = hrp.Orientation

						if newOrientation ~= originalOrientation2 then
							hrp.Anchored = false
							originalOrientation2 = newOrientation
							BodyVelocity2.MaxForce = Vector3.new(0, -100000, 0)
							BodyVelocity2.Velocity = hrp.CFrame.LookVector * 60
							hum.Jump = true
						end
						wait(0.1)
						BodyVelocity2:Destroy()
					end
				end
			end)
		end
	end	
end

Can you format your code properly (```)? This is unreadable.

i know that you have to use backticks but were do i put the backticks