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

How do you make a jump cooldown/delay?

Asked by 4 years ago

Hello! How can you make it so that you only jump once until you release the jump button and at the same time have a cool down? I have no idea how that would work

3 answers

Log in to vote
1
Answered by 4 years ago
Edited 4 years ago
local us = game:GetService("UserInputService")
local canJump = true

us.JumpRequest:Connect(function()
    if canJump then
        canJump = false game.Players.LocalPlayer.Character.Humanoid:SetStateEnabled(Enum.HumanoidStateType.Jumping, false)
        wait(2)
        canJump = true
        game.Players.LocalPlayer.Character.Humanoid:SetStateEnabled(Enum.HumanoidStateType.Jumping, true)
    end
end)

UserInputService has an event which fires each tick when the spacebar is held down, called JumpRequest. Humanoids have a function which allows you to toggle certain controls, like jumping, climbing, walking, running, etc. Setting these to false disables that certain control. Using these, you can make a debounce variable as shown above.

SetStateEnabled, UserInputService

0
How would you make the character jump only one time until the spacebar is released? OddExistent 52 — 4y
0
You would use us.InputEnded:Connect(function(key) and check if the key was spacebar. Utter_Incompetence 856 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

Use the user input service to listen for the space key to be pressed and when the key is pressed change the jump power to however high you want it, wait a few seconds then change the jump power to zero. Don't forget to use debounce, debounce is the key to make the cool down.

0
So that means I have to change the default jump power to 0, and then to 50 when the spacebar is pressed? OddExistent 52 — 4y
0
Yes. Try that. IProgram_CPlusPlus 58 — 4y
0
When i tried that, my character didn't jump. OddExistent 52 — 4y
Log in to vote
0
Answered by 4 years ago

You can make use of debounce. For example, I used the following script for my punch animation. Along with Debounce if you add a wait time after each jump like I have done in my script (for punch) then I think you are good to go.

local Player = game.Players.LocalPlayer
local Character = Player.Character or script.Parent
local Humanoid = Character.Humanoid 
local UserInputService = game:GetService("UserInputService")

local AnimationID = "rbxassetid://4860770751"
local Debounce = true
local key = 'F'

UserInputService.InputBegan:Connect(function(Input, IsTyping)
 if IsTyping then return end
 if Input.KeyCode == Enum.KeyCode[key]and Debounce == true then
  Debounce = false
  local Animation = Instance.new("Animation")
  Animation.AnimationId = AnimationID
  local LoadAnimation = Humanoid:LoadAnimation(Animation)
  LoadAnimation:Play()
  wait(.5)
  Animation:Destroy()
  Debounce = true
 end
end)



https://developer.roblox.com/en-us/articles/Debounce

Answer this question