Problems with making a "hold space to jump higher" mehcanic

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want to make it so when you hold spacebar, a percentage increases. And when you release, you jump with the increased percentage
  2. What is the issue? Include screenshots / videos if possible!
    Getting it to work basically. I’ve tried many things and have gotten many problems. My biggest problem is the player jumping infinitely. Like the boost will work but the player just won’t stop jumping.
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I’ve asked for help in many servers and looked on the developer hub. Unless I’m bad at searching, I’ve found nothing to help me out.

Heres the main code

-- this is inside the keyboard module in player scripts btw
UserInputService.InputBegan:Connect(function(Input, IsTyping)
	if IsTyping then return end

	if Input.KeyCode == Enum.KeyCode.Space then
		CanJump = true
		IncreaseJump = true
	end
end)

UserInputService.InputEnded:Connect(function(Input, IsTyping)
	if IsTyping then return end

	if Input.KeyCode == Enum.KeyCode.Space then
		CanJump = false
		IncreaseJump = false
	end
end)

-- bottom of the script

	local handleJumpAction = function(actionName, inputState, inputObject)
		self.jumpEnabled = CanJump

		local Character = game.Players.LocalPlayer.Character
		local Humanoid: Humanoid = Character:WaitForChild('Humanoid')
		local JumpValue = 16

		self.jumpRequested = self.jumpEnabled and inputState == Enum.UserInputState.End
		self:UpdateJump()

		Humanoid.JumpHeight = JumpValue * (1 + JumpPercentage / 100)
		JumpPercentage = 0

		return Enum.ContextActionResult.Pass
	end

Thanks a lot

Can you provide a video as to how it is infinitely jumping?

https://gyazo.com/5d5949f29205dd3f004a297b9682cc7a
I only pressed jump once

-note: I don’t recommend hijacking roblox’s character control modules

The nonstop jumping is because how the UpdateJump method works

function Keyboard:UpdateJump()
	self.isJumping = self.jumpRequested
end

As you can see, it just sets [isJumping] is true, if [isJumping] is true you will continue to jump.

This is what I would do if I had to handle jumping

local handleJumpAction = function(actionName, inputState, inputObject)
	--only want when ended
	if inputState == Enum.UserInputState.Begin then
		return
	end

	--get instances
	local Character = game.Players.LocalPlayer.Character
	local Humanoid: Humanoid = Character:WaitForChild('Humanoid')

	--set jump height
	local JumpValue = 16
	Humanoid.JumpHeight = JumpValue * (1 + JumpPercentage / 100)

	--handle jump
	self.isJumping = true
	--set to false on next frame
	task.delay(0,function() --idk I can't think of something elegant rn
		self.isJumping = false
	end)

	--reset after jump
	JumpPercentage = 0
	return Enum.ContextActionResult.Pass
end

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.