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

How do I make a sound play when a user double jumps?

Asked by
Aeike 9
7 years ago

Here's my double jump script; which I borrowed from roblox.

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.01
local DOUBLE_JUMP_POWER_MULTIPLIER = 1.5

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)

Now, I've tried multiple methods, and here's my hiearchy https://gyazo.com/7ea2b4e1ad277e8c0799fa5a40a549fa How would I get the sound to play when the user double-jumps? I've tried so many times to do this, but it simply won't work. That is my script so far, so I hope I've complied with the rules. Any help would be appreciated!

1 answer

Log in to vote
0
Answered by 7 years ago

This code should work

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.01
local DOUBLE_JUMP_POWER_MULTIPLIER = 1.5

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) --Forcing the double jump onto the humanoid
    script.Sound:Play()
    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)
Ad

Answer this question