Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to disable the space bar temporarily after jumping?

I want to add a denounce to the space bar when the player jumps, and disable it temporarily. The problem is, I don't know how to do this. Here is my failed attempt.

local character = script.Parent
 
local JUMP_DEBOUNCE = 5
 
local humanoid = character:WaitForChild("Humanoid")
 
local isJumping = false
humanoid.StateChanged:Connect(function(oldState, newState)
	if newState == Enum.HumanoidStateType.Jumping then
		if not isJumping then
			isJumping = true
			humanoid:SetStateEnabled(Enum.HumanoidStateType.Jumping and Enum.KeyCode.Space, false)
		end
	elseif newState == Enum.HumanoidStateType.Landed then
		if isJumping then
			isJumping = false
			wait(JUMP_DEBOUNCE)
			humanoid:SetStateEnabled(Enum.HumanoidStateType.Jumping, true)
		end
	end
end)

1 answer

-- Variables --
local character = script.Parent
local humanoid = character:WaitForChild("Humanoid")
local isJumping = false
local UserInputService = game:GetService("UserInputService")
local DisablingJumpingTime = 5

-- Script --

humanoid.StateChanged:Connect(function(oldState, newState)
	if newState == Enum.HumanoidStateType.Jumping then
		if not isJumping then
			isJumping = true
			wait() -- dont know why, but this wait() fixes it
			humanoid.JumpPower = 0 -- Instead of disabling the jumping, you can change the JumpPower to 0 instead!
		end
	elseif newState == Enum.HumanoidStateType.Landed then
		if isJumping then
			isJumping = false
			wait(DisablingJumpingTime)
			humanoid.JumpPower = 50
		end
	end
end)

4 comments

0

This way is more straightforward and easier. If this helped you, please consider marking this an answer so people will know!

0

If this didn't work, try putting this on StarterPlayer > StarterCharacterScripts and putting this as an LocalScript. Thanks for reading!

0

I appreciate your help but how can this be done with the space bar?

0

Oh, you actually can, just replace the humanoid.JumpPower = 0 to humanoid:SetStateEnabled(Enum.HumanoidStateType.Jumping and Enum.KeyCode.Space, false). I am not really sure since I do not understand a lot of HumanoidStateType. So humanoid jump power is the best way for me!

This discussion is preserved as it was. New answers, comments, and votes are closed.