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

How can I simplify this Double Jump script?

Asked by 5 years ago

Hello, I was using some old Roblox tutorials, because as I said in my previous post, I'm a rookie scripter. So I've come up with this:

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

local canDoubleJump = false
local hasDoubleJumped = false
local oldPowerlocal TIME_BETWEEN_JUMPS = 0.37
local DOUBLE_JUMP_POWER_MULTIPLIER = 1

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)

My question is, is it possible to simplify this script?

1
But why though? Simple isnt always best, sometimes more lines to cover more potential issues is the right route to take. Optimization for the sake of optimization, I don't think you need to fix something if there is no issue. DinozCreates 1070 — 5y
1
Double jump scripts need a lot of state checks so this is normal. User#5423 17 — 5y
0
Yah, this doesnt really need simplfying starmaq 1290 — 5y

Answer this question