Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How to disable the space bar temporarily after jumping?

Asked by 3 years ago

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

Log in to vote
0
Answered by
Xapelize 2658 Moderation Voter Community Moderator
3 years ago
Edited 3 years ago
-- 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)
0
This way is more straightforward and easier. If this helped you, please consider marking this an answer so people will know! Xapelize 2658 — 3y
0
If this didn't work, try putting this on StarterPlayer > StarterCharacterScripts and putting this as an LocalScript. Thanks for reading! Xapelize 2658 — 3y
0
I appreciate your help but how can this be done with the space bar? SilverishReign 75 — 3y
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! Xapelize 2658 — 3y
Ad

Answer this question