How do I make a jump script where if you hold down space you go higher?

This is an example of something I tried:

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

function onKeyPress(inputObject)
	if inputObject.KeyCode == Enum.KeyCode.Space then
		repeat
			wait()
			humanoid.JumpPower = humanoid.JumpPower + 1
		until inputObject.KeyCode == not Enum.KeyCode.Space
		humanoid.Jump = true
		humanoid.JumpPower = 50
	end
end

game:GetService("UserInputService").InputBegan:connect(onKeyPress)

But this ^^^ only makes it so your jump height increases when you jump again and not during the jump.

How can I make something like this?

6 Likes

Yeah, this code makes the jump height increase during the jump. But JumpHeight is only significant when you’re about to jump, not when you’re in the air, so this script essentially does nothing but make successive jumps higher as long as space is held.
There is no bug, it is doing exactly what you told it to do, and you told it to make the next jump higher while you’re holding space.

As for actually making a controllable jump, I searched the internet (which is something you should do, too) and found a few ways to do it.

The first option is to cap off the vertical velocity when you let go of Space. Growtopia had something similar to this.

local jumppower = 50
local releasejumppower = 5

-- spacebar is released, cap the vertical velocity
if HumanoidRootPart.Velocity.Y > releasejumppower then
	HumanoidRootPart.Velocity = Vector3.new(HumanoidRootPart.Velocity.X, releasejumppower, HumanoidRootPart.Velocity.Z)
end

Another option, seemingly used in earlier Mario games, is to have different gravity while the jump key is held and while it is not held.
Setting Workspace.Gravity won’t work well because it will affect everything the player has control over, i.e. often not just the character. A BodyForce in the character will work fine for only reducing the character’s gravity.

3 Likes

You can try this:

-- disable jumps for current character
local humanoid = game.Players.LocalPlayer.Character:WaitForChild("Humanoid") -- wait for the humanoid to be added
humanoid:GetPropertyChangedSignal("Jump"):Connect(function() -- listen for jumps
	humanoid.Jump = false -- stop the jump before it can happen
end)

-- disable jumps for future characters
game.Players.LocalPlayer.CharacterAdded:Connect(function(character)
	local humanoid = character:WaitForChild("Humanoid") -- wait for the humanoid to be added
	humanoid:GetPropertyChangedSignal("Jump"):Connect(function() -- listen for jumps
		humanoid.Jump = false -- stop the jump before it can happen
	end)
end)

local UserInputService = game:GetService("UserInputService")
local Heartbeat = game:GetService("RunService").Heartbeat

UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
	if input.KeyCode == Enum.KeyCode.Space and not gameProcessedEvent then -- checking for gameProcessedEvent will get rid of cases where space isn't being pressed to jump (e.g in chat)
		local humanoid = game.Players.LocalPlayer.Character and game.Players.LocalPlayer.Character:FindFirstChildOfClass("Humanoid") -- checks that the player actually has a character and humanoid so the script doesn't error
		
		while UserInputService:IsKeyDown(Enum.KeyCode.Space) do  -- check if space is being held down
			humanoid.JumpPower += 1 -- you might want to add a limit to this or players are going to be jumping very, very high (you might also be wondering what this += is, read a bit about what it is here https://devforum.roblox.com/t/42142
			Heartbeat:Wait() -- heartbeat:Wait() is faster and usually more reliable than wait() (you should avoid using wait() in your game's code)
		end
		
		humanoid.Jump = true -- make the humanoid jump
		humanoid.JumpPower = 50 -- set jump power back to normal
	end
end)

It first disables jumping for the character, and then makes the character jump after they’ve pressed or held down space. The longer you hold down space, the higher your jump will end up being (although even if a player just tapped spacebar, this would still make their jump a tiny bit higher)

1 Like

I don’t know if this video is close to the thing you want to do

Use BodyVelocity to perform such a jump

If you press and hold the Space key, the player continues go higher

InputBegan:Connect

Create BodyVelocity

BodyVelocity.Velocity = HumanoidRootPart.CFrame.UpVector * 5

If you stop pressing key Space, the player will fall to the ground

InputEnded:Connect

BodyVelocity:Destroy()

1 Like

That’s exactly what I want! But you can’t move while in the air. Do you know how I can make the character be able to move while in the air?

To move the character forward

InputBegan

input.KeyCode == Enum.KeyCode.Up

BodyVelocity.Velocity = HumanoidRootPart.CFrame.lookVector * 5

To move the character to the back

InputBegan

input.KeyCode == Enum.KeyCode.Down

BodyVelocity.Velocity = HumanoidRootPart.CFrame.lookVector * -5

Either way, you should restore to the previous position when you stop pressing both keys Up and Down

InputEnded

BodyVelocity.Velocity = HumanoidRootPart.CFrame.UpVector * 5

And if you want to fly then this link

You need some kind of function that will Reset the jump action

function onSpaceHold()
repeat
if jumpPower <= jumpPowerMax and spaceDown() then
jumpPower += 1
end
until character.isGrounded()
jumpPower = defaultJumpPower
end

Allow me to explain:

  • First you need to have a function to do all of the jumping action
  • Then you need to have to keep repeating inside the function until the character is grounded on a anchored object
  • Make sure to have the condition fire when the jump power isn’t hitting the max jump power and has to hold space for it to fire
    *Then finally, make sure the jumppower increases everytime the condition is meet. Also reset the jump power after the repeat until condition.

Let me know if this worked.

3 Likes