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

This script from a wiki article I'm copying does not make me double jump?

Asked by 6 years ago

Hello, I was trying to make my own copy of this wiki article by typing it up myself instead of copy/paste, however, it doesn't work! It doesn't show an error in the output, but the player cannot use the double jump ability.

Here is my code:

local UserInputService = game:GetService("UserInputService")
local localPlayer = game.Players.LocalPlayer
local character
local humanoid

local canDoubleJump = false
local hasDoubleJumped = false
local oldPower
local TIME_BETWEEN_JUMPS = 0.2
local DOUBLE_JUMP_POWER_MULTIPLIER = 2

function onJumpRequest()
    if not character or not humanoid or not character:IsDescendantOf(workspace) or
         humanoid:GetState() == Enum.HumanoidStateType.Dead then
        return
    end

    if canDoubleJump and not hasDoubleJumped then
        hasDoubleJumped = true
        humanoid.JumpPower = oldPower * DOUBLE_JUMP_POWER_MULTIPLIER
        humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
    end
end

local function characterAdded(newCharacter)
    character = newCharacter
    humanoid = newCharacter:WaitForChild("Humanoid")
    hasDoubleJumped = false
    canDoubleJump = false
    oldPower = humanoid.JumpPower

    humanoid.StateChanged:connect(function(old, new)
    if new == Enum.HumanoidStateType.Landed then
        canDoubleJump = false
        hasDoubleJumped = false
        humanoid.JumpPower = oldPower
    elseif new == Enum.HumanoidStateType.Freefall then
        wait(TIME_BETWEEN_JUMPS)
        canDoubleJump = true
    end
    end)
end

if localPlayer.Character then
    characterAdded(localPlayer.Character)
end

localPlayer.CharacterAdded:connect(characterAdded)
UserInputService.JumpRequest:connect(onJumpRequest)

And for comparison, the Wiki article code:

local UserInputService = game:GetService("UserInputService")
local localPlayer = game.Players.LocalPlayer
local character
local humanoid

local canDoubleJump = false
local hasDoubleJumped = false
local oldPower
local TIME_BETWEEN_JUMPS = 0.2
local DOUBLE_JUMP_POWER_MULTIPLIER = 2

function onJumpRequest()
    if not character or not humanoid or not character:IsDescendantOf(workspace) or
     humanoid:GetState() == Enum.HumanoidStateType.Dead then
        return
    end

    if canDoubleJump and not hasDoubleJumped then
        hasDoubleJumped = true
        humanoid.JumpPower = oldPower * DOUBLE_JUMP_POWER_MULTIPLIER
        humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
    end
end

local function characterAdded(newCharacter)
    character = newCharacter
    humanoid = newCharacter:WaitForChild("Humanoid")
    hasDoubleJumped = false
    canDoubleJump = false
    oldPower = humanoid.JumpPower

    humanoid.StateChanged:connect(function(old, new)
        if new == Enum.HumanoidStateType.Landed then
            canDoubleJump = false
            hasDoubleJumped = false
            humanoid.JumpPower = oldPower
        elseif new == Enum.HumanoidStateType.Freefall then
            wait(TIME_BETWEEN_JUMPS)
            canDoubleJump = true
        end
    end)
end

if localPlayer.Character then
    characterAdded(localPlayer.Character)
end

localPlayer.CharacterAdded:connect(characterAdded)
UserInputService.JumpRequest:connect(onJumpRequest)
0
did you put it in a localscript? radusavin366 617 — 6y

1 answer

Log in to vote
0
Answered by
chasedig1 115
6 years ago

canDoubleJump is false.

1
I knew something was wrong! Thanks. AtomicChocolate 35 — 6y
Ad

Answer this question