Hello! I am still a relatively new Roblox Scripter (only been learning luau for a few months)
I am trying to script a double jump system for my game; however whenever I only jump once and not twice in a row; it still double jumps! It’s in StarterCharacterScripts and is a LocalScript.
In the video; I am only holding jump for a slight amount of time then releasing; but not tapping jump again in the middle of my jump.
Here is the code that is related to this issue:
debounce = false
jumpAmount = 0
hum = script.Parent.Humanoid
uis = game:GetService("UserInputService")
uis.JumpRequest:Connect(function()
if hum:GetState() == Enum.HumanoidStateType.Dead then return end
if debounce == false and hum:GetState() == Enum.HumanoidStateType.Freefall then
jumpAmount += 1
print(jumpAmount)
task.wait((2 * hum.JumpHeight / ((workspace.Gravity*hum.JumpPower) * 2) + 0.5) / 2)
if jumpAmount == 1 then return end
debounce = true
hum.JumpPower = hum.JumpPower * 5
hum:ChangeState(Enum.HumanoidStateType.Jumping)
repeat wait() until hum:GetState() == Enum.HumanoidStateType.Landed
jumpAmount = 0
debounce = false
end
end)
Only here you reset the jumpAmount. So each time a player jumps the jumpamount increases by 1.
Let me give you an example to make it more simple:
Player Jumps
jumpAmount += 1 (now the jumpAmount is equal to 1) Player Jumps again (not double jump)
jumpAmount += 1 (now the jumpAmount is equal to 2, so now the player does a double jump) Player Jumps again
jumpAmount += 1 (now the jumpAmount is equal to 1, because jumpAmount is resetted after a double jump)
To fix this you can edit the following part:
if jumpAmount == 1 then return end
to:
if jumpAmount == 1 then
jumpAmount = 0
return
end
Now, when a player jumps onces, it will reset the jumpamount and return so the double jump is not executed.
An if the player holds the space button it will do a double jump.
Hope this helped you, if you have any other questions let me know!