Double jump script not working

here’s the script:

local plr = game.Players.LocalPlayer
local char = plr.Character
local hasJumped = false
local canJump = false
local uis = game:GetService("UserInputService")
local hum = char.Humanoid

hum:GetPropertyChangedSignal("Jump"):Connect(function()
	if not hasJumped then
		if canJump then
			local connection
			connection = uis.InputBegan:Connect(function(input, gpe)
				if not gpe then
					if input.KeyCode == Enum.KeyCode.Space then
						print("you double jumped!")
						connection:Disconnect()
						char.HumanoidRootPart:ApplyImpulse(
							(Vector3.new(char.HumanoidRootPart.CFrame.LookVector.X, 1,
							char.HumanoidRootPart.CFrame.LookVector.Z)) * 300)
						hasJumped = true
					end
				end
			end)
		end
	end
end)

hum.StateChanged:Connect(function(state)
	if state == Enum.HumanoidStateType.Jumping then
		canJump = true
	elseif state == Enum.HumanoidStateType.Freefall then
		canJump = false
	elseif state == Enum.HumanoidStateType.Landed then
		hasJumped = false
	end
end)

everytime i hold the jump button down, then release and press one more time, it jumps extremely far!

Here is a link for Double Jumping which should help.

Double Jumping.