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

Trying to make a jump height based on button hold duration?

Asked by 6 years ago
Edited 6 years ago

I am trying to make this script through the magnitude of the humanoidrootpart of the player instead of userinput. (I believe adding specific keyboard controls are inefficient since I want this script to work for all roblox platform controls like xbox and mobile). different platforms have different controls.

What I am trying to do. Something like this, (A quote from reddit.com)

" **When I press the jump button, I set my vertical speed to a negative value. Then I check to see if the jump button is being held. --Again, I'm using magnitude to trigger jump not UserInput, it should not matter which "jump button" you are pressing on different platforms.-- If the jump button is not being held, and our vertical speed is negative (means we're still going up), I divide the vertical speed by 1.5 every step. This makes it so you stop going up when you release the jump button, but it's not a completely abrupt stop, so it looks more natural."

** Here is my cruddy attempt which doesn't work correctly. I tried using gravity and jumpPower but it still a no go. Please help me with this please?!

local humanoid = script.Parent
local humanoidrootpart = humanoid.Parent:WaitForChild("HumanoidRootPart")
game:GetService( "RunService" ).Heartbeat:connect( function()
if humanoidrootpart.Velocity.Magnitude > 1 then
humanoid.JumpPower = humanoid.Power + 1
game.WorkSpace.Gravity = game.WorkSpace.Gravity - 1

else
 game.WorkSpace.Gravity  = 900 -- Default Gravity
humanoid.JumpPower = 75 -- Default Jump Power
end
end)

1 answer

Log in to vote
0
Answered by
movsb 242 Moderation Voter
6 years ago

You can use ROBLOX's UserInputService for both PC, and XBox keyboard/controller input. Here is a link to Gamepad input if you do not already know about it http://wiki.roblox.com/index.php?title=Gamepad_input

As for your script, I have one good idea of what you could do. Theoretically you could get input through userinputservice, spawn a function that continuously checks how long certain keys/buttons remain down for, and from there depending on how long they remain down change the humanoid's state type back to jumping, essentially allowing the character to continue jumping as long as a key or button is being held down. Now actually programming that will be a different story. I will use Enum.KeyCode.Space, and Enum.KeyCode.ButtonA for controls.

local plr = game.Players.LocalPlayer;
local jumped = false;
local key;
local debounce = false;

local function IsKeyDown(keyCode)
    local is_down = false;
    local keys = game:GetService("UserInputService"):GetKeysPressed();
    for i,v in pairs(keys) do
        if v.KeyCode == keyCode then
            is_down = true;
        end
    end
    return is_down;
end

local jump_limit = 5; --the max amount of times a player can 're-jump'

spawn(function()
    local local_limit = 0; --the local counter
    while wait() do
        if jumped then
            while IsKeyDown(key) do
                pcall(function()
                    plr.Character.Humanoid:ChangeState(
                        Enum.HumanoidStateType.Jumping);
                end);
                local_limit = local_limit + 1;
                wait(1 / 5); --time between each jump
                if local_limit >= jump_limit then
                    local_limit = 0;
                    break;
                end
            end
            jumped = false;
            key = nil;
        end
        debounce = false;
    end
end);

game:GetService("UserInputService").InputBegan:connect(function(input)
    if not debounce then
        debounce = true;
        if input.KeyCode == Enum.KeyCode.Space or
            input.KeyCode == Enum.KeyCode.ButtonA then
            key = input.KeyCode;
            jumped = true;
        end
    end
end);

Now how to actually get this script to work is a whole other story. First, make sure that the script is a LocalScript inside some part of the client (Preferably the client's StarterPlayerScripts), Lastly you may have to disable or remove ROBLOX movement script(s), or use this as ROBLOX's movement script for all clients. (You can do so by naming the localscript "ControlScript" and placing it in StarterPlayerScripts) You may need to do this because of the fact that ROBLOX uses Enum.KeyCode.Space for jumping as well, and it will most likely not overlap smoothly, although you can always change the KeyCodes in InputBegan to a keycode that roblox does not use for movement.

Finally

If you do not want there to be a limit on how much a player can jump then simply remove lines 17, 20, 28, 30, 31, 32, and 33. You can change how smooth the jump transitions are by changing the wait time on line 29. You can also edit the jump limit to however many extra times you want the player to be able to jump. I hope this helped you.

Ad

Answer this question