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

Charged jump only works every second time?

Asked by
WoTrox 345 Moderation Voter
2 years ago

I tried adding a charged jump function into my game, which on short press makes the player jump normally, but when held down makes the player jump higher depending on how much time it was held for.

hum:SetStateEnabled(Enum.HumanoidStateType.Jumping, false);

uis.InputBegan:Connect(function(input, t)
    if t then return end
    if input.UserInputType == Enum.UserInputType.Keyboard then
        local key = input.KeyCode;

        if key == Enum.KeyCode.Space and not(shiftdown) and not(inair) then
            if canJump and stamina.Value > 15 then
                spacedown = true;
                print("down")
                while spacedown do
                    counter += 0.1;
                    wait(0.1);
                end
            elseif stamina.Value > 5 then
                hum.JumpPower = 25;
                hum.Jump = true;
            end

        else (...)
        end
    end
end)

uis.InputEnded:Connect(function(input, t)
    if t then return end
    if input.UserInputType == Enum.UserInputType.Keyboard then
        local key = input.KeyCode;

        if key == Enum.KeyCode.Space and spacedown then
            hum:SetStateEnabled(Enum.HumanoidStateType.Jumping, true);
            hum.JumpPower = math.clamp(counter*20, 25, 60);
            hum.Jump = true;
            spacedown = false;
            counter = 0;
            inair = true;
            print("up")
        else (...)
        end
    end
end)

hum.StateChanged:Connect(function(old, new)
    if (old == Enum.HumanoidStateType.Freefall) or (new == Enum.HumanoidStateType.Landed) then
        if not(spacedown) then
            hum:SetStateEnabled(Enum.HumanoidStateType.Jumping, false);
            print("landed")
            inair = false;
        end
    end
end)

The player only jumps every second time, and always a small jump, no matter how long I hold it down. There are no errors in the output, and "up" and "down" only appear when the player doesn't jump. Any ideas why it doesn't work?

0
Can you explain the unnecessary locals such as `key`? Also, you shouldn't use SetStateEnabled to change the state, instead use ChangeState and SetStateEnabled Jumping to false outside the other scopes. I will help later just not now, busy. Antelear 185 — 2y

Answer this question