Double Jump Issues

I have a double jump script and was just wondering if anyone could help improve it to fix an issue that I am experiencing. All I need is for someone to add something to the script that cuts the animation short if the player is already on the ground. I want this done because if you double jump to a high place and land the rest of the animation will continue playing even after you land. Thank you for your help, just ask if you need more information.

Here is the script:

local UIS = game:GetService("UserInputService")
local CanDoubleJump = true
local CanSetCanDoubleJumpToTrue = false
local Character = game.Players.LocalPlayer.Character or game.Players.LocalPlayer.CharacterAdded:Wait()

UIS.JumpRequest:Connect(function()
    if CanDoubleJump then
        Character:WaitForChild("Humanoid").JumpPower = Character:WaitForChild("Humanoid").JumpPower * 1
        Character:WaitForChild("Humanoid"):ChangeState(Enum.HumanoidStateType.Jumping)
        local Animator = Character:WaitForChild("Humanoid"):FindFirstChild("Animator") or Instance.new("Animator",Character:WaitForChild("Humanoid"))
        Animator:LoadAnimation(script.Flip):Play()
        CanDoubleJump = false
    end
end)

game.Players.LocalPlayer.Character:WaitForChild("Humanoid").StateChanged:Connect(function(old,new)
    if new == Enum.HumanoidStateType.Landed then
        CanDoubleJump = false
        CanSetCanDoubleJumpToTrue = true
        Character:WaitForChild("Humanoid").JumpPower = game:GetService("StarterPlayer").CharacterJumpPower
    elseif new == Enum.HumanoidStateType.Freefall then
        if CanSetCanDoubleJumpToTrue == true then
            wait(0.2)
            CanDoubleJump = true
            CanSetCanDoubleJumpToTrue = false
        end
    end
end)

Again, thank you.

here try this:

if humanoid:GetState == Enum.HumanoidStateType.Landed or humanoid:GetState == Enum.HumanoidStateType.Running then
     if doubleJump.IsPlaying then
          doubleJump:Stop()
     end
end
2 Likes

Basically this script checks if you’re jumping or already on the ground and if so checks if anim is playing. If it is it makes sure it stops.

Funny thing, I had the same problem just 20 minutes ago haha

Doesn’t work, I forgot to inform you but its a local script in StarterPack

Still stick it in there to make sure, I’ll try and figure something else for you :grin:

Looks like this and it stops the script from working at all.

Just saw this, try using FloorMaterial

e.g. if BasePart.FloorMaterial == Enum.FloorMaterial.Air (In the air)

What’s your value for the players hunanoid? Also make sure you rewrite my code cuz I am typing on phone, my code won’t work.

Replace “humanoid” and “doubleJump” with your values.

Here is a working script, enjoy.


local UIS = game:GetService("UserInputService")
local Players = game:GetService("Players")
local StarterPlayer = game:GetService("StarterPlayer")

local player = Players.LocalPlayer
local Character = player.Character or player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")

type Animator = Animator & {
    LoadAnimation: (self: Animator, animation: Animation) -> AnimationTrack,
}

type AnimationTrack = AnimationTrack & {
    Play: (self: AnimationTrack) -> nil,
    Stop: (self: AnimationTrack) -> nil,
    IsPlaying: boolean,
}

local CanDoubleJump: boolean = true
local CanSetCanDoubleJumpToTrue: boolean = false
local DoubleJumpAnimation: AnimationTrack? = nil
local Animator: Animator? = nil

local function loadDoubleJumpAnimation(): AnimationTrack
    if not DoubleJumpAnimation then
        Animator = Humanoid:FindFirstChild("Animator") or Instance.new("Animator", Humanoid)
        DoubleJumpAnimation = Animator:LoadAnimation(script.Flip)
    end
    return DoubleJumpAnimation
end

local function handleJumpRequest()
    if CanDoubleJump then
        Humanoid.JumpPower = Humanoid.JumpPower * 1
        Humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
        local animation = loadDoubleJumpAnimation()
        animation:Play()
        CanDoubleJump = false
    end
end

local function handleStateChange(oldState: Enum.HumanoidStateType, newState: Enum.HumanoidStateType)
    if newState == Enum.HumanoidStateType.Landed then
        CanDoubleJump = false
        CanSetCanDoubleJumpToTrue = true
        Humanoid.JumpPower = StarterPlayer.CharacterJumpPower
        if DoubleJumpAnimation and DoubleJumpAnimation.IsPlaying then
            DoubleJumpAnimation:Stop()
        end
    elseif newState == Enum.HumanoidStateType.Freefall then
        if CanSetCanDoubleJumpToTrue then
            task.wait(0.2)
            CanDoubleJump = true
            CanSetCanDoubleJumpToTrue = false
        end
    end
end

local function onJumpRequestError(err)
    warn("Error in handleJumpRequest: " .. tostring(err))
end

local function onStateChangeError(err)
    warn("Error in handleStateChange: " .. tostring(err))
end

UIS.JumpRequest:Connect(function()
    local success, err = pcall(handleJumpRequest)
    if not success then
        onJumpRequestError(err)
    end
end)

Humanoid.StateChanged:Connect(function(oldState, newState)
    local success, err = pcall(handleStateChange, oldState, newState)
    if not success then
        onStateChangeError(err)
    end
end)

3 Likes

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