I want to add a “Charge Jump” into my game. I want to have it so if the player holds space for 2 seconds, they will then jump forward with increased jump power.
However, I am already running into an issue from the beginning on figuring out how to get the input checks correctly. I already have a Double Jump script in my game, so this may be getting in the way of this, but I currently have it disabled so I can figure the Charge Jump out.
I will eventually have them both in the same script but want to make sure they both work correctly individually
Basically, if the player begins holding space, and is still holding it 2 seconds later, then they will jump forward. Something similar to Shindo Life’s tree jumping.
This is what I currently have, but is not working, as the player jumps as soon as space is pressed.
Summary
UIS.InputBegan:Connect(function(input)
if State ~= "onRunning" then
return
end
if State == "onRunning" then
print("Space hold began")
if input.Keycode == Enum.KeyCode.Space then
wait(1.5)
if input.Keycode == Enum.KeyCode.Space then
print("Space hold working")
end
end
end
end)
I am checking state to make sure the player is grounded and not in the air
Like I said above, I believe the problem is that the player is jumping when space is pressed, which is why the script is not getting through the State check.
How would I check if it is down for 2 seconds, and also stop the player from jumping when they press space down?
I imagine I would have to find a way to tell if the player just tapped space (they would jump), or if they are holding it?
I changed the script to this, but the Print is not printing
local SpaceHeld = UIS:IsKeyDown(Enum.KeyCode.Space)
if SpaceHeld then
wait(1.5)
humanoid.JumpPower = oldPower * jump_multi
print("Space hold working")
end
The main problem with IsKeyDown is that I don’t think that there would be an easy way of checking how long a user has pressed a key down for. Instead, I decided to use InputBegan and its sibling InputEnded.
I create a variable when the player starts holding space, which is just the time (Lua has a built-in function time() which gives you the current time). When they let go, I check to see if they held down the button long enough. If they did, then I change their JumpPower, otherwise I don’t.
How do I stop players from jumping on their own? Well, I use SetStateEnabled and disable their jump, then manually enable and force them to jump using ChangeState when they let go.
if time() - originalTime >= 2 then ... end
Then, I use StateChanged on the humanoid to check when they land (it should be Enum.HumanoidStateType.Landed as the new state, to reset the JumpPower if it has changed.
Everything is wrapped up in a LocalScript inside StarterCharacterScripts since we are changing that.
Here’s the demo of it in action:
The main problem I have with my solution is that:
There’s a small but noticeable amount of input delay (the time between pressing the button and having your character jump). I’m not sure if there’s a solution to fix that exactly.
This would probably be pretty easy to abuse and exploit, but that’s just Roblox humanoids.
Perhaps there’s a better solution out there. Hope I helped!
Here is the code that I've written. It's not pretty, but it works. I encourage you to try it on your own first!
local Character = script.Parent
local Humanoid = Character:FindFirstChild("Humanoid") or Character:WaitForChild("Humanoid")
local jumpInputTime
game:GetService("UserInputService").InputBegan:Connect(function(input, gameProcessed)
if input.UserInputType == Enum.UserInputType.Keyboard and input.KeyCode == Enum.KeyCode.Space then
Humanoid:SetStateEnabled(Enum.HumanoidStateType.Jumping, false)
jumpInputTime = time()
end
end)
game:GetService("UserInputService").InputEnded:Connect(function(input, gameProcessed)
if input.UserInputType == Enum.UserInputType.Keyboard and input.KeyCode == Enum.KeyCode.Space then
Humanoid:SetStateEnabled(Enum.HumanoidStateType.Jumping, true)
if time() - jumpInputTime >= 2 then
print("JumpPower changed!")
Humanoid.JumpPower *= 3
end
Humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
end
end)
Humanoid.StateChanged:Connect(function(oldState, newState)
if newState == Enum.HumanoidStateType.Landed then
if Humanoid.JumpPower ~= 50 then Humanoid.JumpPower = 50 end
end
end)
The only problem that I would have with using this, is that I am using Gravity Controller, and every player’s HumanoidStateType is set to PlatformStand constantly.
I have found success using the StateTracker included within the Gravity Controller kit, but when it comes to the issues I am having with this charge jump, I am not sure how to get around it.
(You can get the gravity controller through the hyperlink as it is a free system and open sourced, if you would like to know more about why I am using a different StateTracker)
The only problem I currently have is disabling Jump on Space press while also being able to reenable it somehow. Here is a GIF showing that the print function of the script is working in output: https://gyazo.com/3d2a5836fea0b08f58550289f5b82665
Here is the script that I currently have; Im sure it just needs a few changes when compared to yours but I’m not sure where to begin since I’ve only been scripting for a few weeks haha
It is something with setting humanoid.Jump = false but I am unsure of another way to do this
Summary
local player = game.Players.LocalPlayer
local UIS = game:GetService("UserInputService")
local character = player.Character
local humanoid = character:WaitForChild("Humanoid")
local oldPower
local jump_multi = 2
local State = character:WaitForChild("State")
local inputTime
humanoid.JumpPower = oldPower
UIS.InputBegan:Connect(function(input, gameProcessed)
if input.UserInputType == Enum.UserInputType.Keyboard and input.KeyCode == Enum.KeyCode.Space then
humanoid.Jump = false
inputTime = time()
end
end)
UIS.InputEnded:Connect(function(input, gameProcessed)
if input.UserInputType == Enum.UserInputType.Keyboard and input.KeyCode == Enum.KeyCode.Space then
humanoid.Jump = true
if time() - inputTime >= 1 then
print("Charge Jump Worked")
humanoid.JumpPower = 50 * jump_multi
end
humanoid.Jump = true
end
end)