Help with jump script

Hey so I’m making a script where once the player stops holding down space the jump ends so your jump height depends on how long you held the space key.

This script is not smooth and if you spam space you glide in the air.

How can I fix and improve the script?

StarterPlayer >> StarterCharacterScripts >> Folder >> LocalScript

local UserInputService = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local character = player.Character
local humanoid = character.Humanoid

UserInputService.InputEnded:Connect(function(key)
	if key.KeyCode == Enum.KeyCode.Space then
		character.HumanoidRootPart.Velocity = Vector3.new(		 
            character.HumanoidRootPart.Velocity.X,
			character.HumanoidRootPart.CFrame.UpVector * 20,
			character.HumanoidRootPart.Velocity.Z)
	end

end)

1 Like

I suggest you also use UserInputService.InputBegan to do this, e.g

local power = 0
local keyDown = false

UserInputService.InputBegan:Connect(function(key)
    if (key.KeyCode == Enum.KeyCode.Space) then
        keyDown = true -- set keydown to true
        power = 0 -- set power back to 0 

        while (keyDown) do -- while keydown
              wait(0.1) 
              power = power + 1 -- increase power
        end
    end
end)

UserInputService.InputEnded:Connect(function(key)
    if key.KeyCode == Enum.KeyCode.Space then
        keyDown = false -- stops the loop adding power
	    character.HumanoidRootPart.Velocity = Vector3.new(		 
            character.HumanoidRootPart.Velocity.X,
		    character.HumanoidRootPart.CFrame.UpVector * power, -- and boom, jumps as high as you held the key down for
		    character.HumanoidRootPart.Velocity.Z
        )
    end
end)
1 Like

Thanks! ~~~~~~~~~~~~~~~~~~~~~~

For those looking at this thread in the future you’re gonna wanna add this on line 3:

character:WaitForChild("Humanoid").StateChanged:Connect(function(OldState,NewState)
	if NewState == Enum.HumanoidStateType.Freefall then
		keyDown = false
	end
end)

and remove keyDown = false on line 17 :wink: