Crawl Script not letting the Player Stand

Alright so my goal is to have a crawl script that lets the player crawl while the leftshift key is held down (while they’re stood still for clarification). Good news, I got that far, bad news, I only want them to be able to stand up if there’s enough room above them for them to stand up, a few parts or something.

I found a script here on the devforum to do this, but it’s just caused my character to permanently be crawling and the player won’t stand up at all now so.

Here’s the entire script:

local UserInputService = game:GetService("UserInputService")
local player = script.Parent -- in this case, the player will be the parent of the script.
local hum = player:WaitForChild("Humanoid")
local anim = hum:LoadAnimation(script:WaitForChild("Animation")) --If you renamed your animation, then consider changing the "Animation" to your animation name!

hum.Running:Connect(function(speed)
	if speed > 0 then
		anim:AdjustSpeed(1)
	else
		anim:AdjustSpeed(0)
	end
end)

local crawling = false -- debounce


local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Character = Player.Character
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")

local RayDirection = Vector3.new(0, 100, 0)

local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {Character}
raycastParams.FilterType = Enum.RaycastFilterType.Exclude

local MinimumGapSize = 5

function CheckHeightAbove()
	local RayOrigin = HumanoidRootPart.Position
	local RaycastResult = workspace:Raycast(RayOrigin, RayDirection, raycastParams)

	if RaycastResult then
		local HitPart = RaycastResult.Instance
		if HitPart:IsA("BasePart") then
			local PartY = HitPart.Position.Y
			local HrpY = HumanoidRootPart.Position.Y
			local Difference = PartY - HrpY
			if Difference > MinimumGapSize then
				return false
			else
				return true
			end
		end
	end
end


UserInputService.InputBegan:Connect(function(input, isTyping)
	if not isTyping then -- if the player isn't typing in the chat
		if input.KeyCode == Enum.KeyCode.LeftShift and hum.MoveDirection == Vector3.new(0,0,0) then
			if not crawling then
				crawling = true
				anim:Play()
				anim:AdjustSpeed(0)
				hum.WalkSpeed = 16
				hum.JumpPower = 0 -- NOTE: Head into StarterPlayer, and set "CharacterUseJumpPower" to true, since it's using "CharacterJumpHeight" currently.
				hum.HipHeight = 0 -- yes, you have to set this to 0.3, or else you'll be floating in the air. (if you're making an R6 animation, then change this to -0.3)	
			end
		end
	end
end)

UserInputService.InputEnded:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		if crawling then
			local canStand = CheckHeightAbove()
			
			if canStand == true then
				anim:Stop()
				hum.WalkSpeed = 35
				hum.JumpPower = 50
				hum.HipHeight = 0
				crawling = false
			else
				repeat wait(.1) until canStand == true
				anim:Stop()
				hum.WalkSpeed = 35
				hum.JumpPower = 50
				hum.HipHeight = 0
				crawling = false
			end
		end
	end
end)

You can probably filter out the bits you don’t need, messy I know don’t worry, I’ve merged together two scripts so it’s a little sloppy. Thanks for the help in advance (if anyone decides to help this time LOL)

Shouldn’t this be returning true if the distance between the player > gapsize?

			local PartY = HitPart.Position.Y
			local HrpY = HumanoidRootPart.Position.Y
			local Difference = PartY - HrpY
			if Difference > MinimumGapSize then
				return false
			else
				return true
			end