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 7 years ago
Edited 7 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?!

01local humanoid = script.Parent
02local humanoidrootpart = humanoid.Parent:WaitForChild("HumanoidRootPart")
03game:GetService( "RunService" ).Heartbeat:connect( function()
04if humanoidrootpart.Velocity.Magnitude > 1 then
05humanoid.JumpPower = humanoid.Power + 1
06game.WorkSpace.Gravity = game.WorkSpace.Gravity - 1
07 
08else
09 game.WorkSpace.Gravity  = 900 -- Default Gravity
10humanoid.JumpPower = 75 -- Default Jump Power
11end
12end)

1 answer

Log in to vote
0
Answered by
movsb 242 Moderation Voter
7 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.

01local plr = game.Players.LocalPlayer;
02local jumped = false;
03local key;
04local debounce = false;
05 
06local function IsKeyDown(keyCode)
07    local is_down = false;
08    local keys = game:GetService("UserInputService"):GetKeysPressed();
09    for i,v in pairs(keys) do
10        if v.KeyCode == keyCode then
11            is_down = true;
12        end
13    end
14    return is_down;
15end
View all 51 lines...

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