So, In my game, players can double jump by pressing jump once, then another jump in mid air. This works perfectly fine though there were some problems.
The first double jump script I used was the one provided by the roblox developer hub: Documentation - Roblox Creator Hub, I knew how it worked though, when the player jumps, then is in freefall, and the player decides to press the space key again, it fires the jump event again causing the player to essentially double jump in mid air. Theres also a debounce on it to make it only work once then have to be reinstated by landing on the ground. Though thats where the problem arises.
My game has many jumps and slopes. When the player happens to fall just a little after running up a slope or jumping right in between two parts, if the player decides to jump, it automatically does a double jump causing the player to get frustrated when they realize they cant double jump since it just broke.
So I decided to see if there was an alternative, there was one:
local UserInputService = game:GetService("UserInputService")
local character = script.Parent
local humanoid = character:WaitForChild("Humanoid")
local doubleJumpEnabled = false
humanoid.StateChanged:Connect(function(oldState, newState)
if newState == Enum.HumanoidStateType.Jumping then
if not doubleJumpEnabled then
wait(.2)
if humanoid:GetState() == Enum.HumanoidStateType.Freefall then
doubleJumpEnabled = true
end
end
elseif newState == Enum.HumanoidStateType.Landed then
doubleJumpEnabled = false
end
end)
UserInputService.InputBegan:Connect(function(inputObject)
if inputObject.KeyCode == Enum.KeyCode.Space then
if doubleJumpEnabled then
if humanoid:GetState() ~= Enum.HumanoidStateType.Jumping then
humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
spawn(function()
doubleJumpEnabled = false
end)
end
end
end
end)
Essentially how this code worked was by detecting if the player jumped, if they did, then double jump is enabled while in freefall, during that time, if the player decides to want to double jump, the part below detects when the player presses the SpaceBar (Important later on). Which works perfectly and makes doublejumpenabled = false when the player lands which fixed the problem that was ontop. But then I run into another issue, I realize that mobile players cant double jump since it only detects if someone presses the spacebar. I tried searching online for a way to track if the player presses the jump button on mobile but couldn’t find an answer.
Is there a way I could make a more reliable double jump system or a way to detect if a mobile player presses the jump button?