I currently have a script that disables jump for a temporary amount of time (aka a cooldown). This works on PC devices fine, but not on mobile, the jump button disappears forever.
I believe this is because setting HumanoidStateType.Jumping to false (the method used in the tutorial on this page: Humanoid | Roblox Creator Documentation) disables the jump button forever on mobile.
I have tried using ModalEnabled to fix this but it has not worked.
InputService = game:GetService("UserInputService")
plr = game.Players.LocalPlayer
repeat wait() until plr.Character and plr.Character.Parent == game.Workspace
char = plr.Character or plr.CharacterAdded:Wait()
hum = char:WaitForChild("Humanoid")
lastJump = 0
function Jump()
local now = tick()
local elapsed = now - lastJump
if elapsed > 1.2 then
lastJump = now
hum:SetStateEnabled(Enum.HumanoidStateType.Jumping, true)
else
hum:SetStateEnabled(Enum.HumanoidStateType.Jumping, false)
end
end
InputService.InputBegan:Connect(Jump)
Is there a way to fix this?
Many thanks if you can provide any help
Edit: end should be InputService.JumpRequest:Connect(Jump) not InputBegan
Were you testing this on a phone or in studio. Because if you use the keyboard the jump button will disappear all you should have to do though is press on the screen again
The issue stems from how the function is being called and how that call handles itself. In the provided code, InputBegan fires from anything, so any two inputs–regardless of what they are–will enable the jumping state if triggered more than 1.2 seconds apart or disable it if less than or equal to that amount. Tapping the screen too frequently disables jump, but it will work again if you wait a bit longer before tapping again. The same should be true of any two key presses on PC, although there would be no UI to differentiate the enabled/disabled states.
@ZykoreK’s suggestion will work on mobile. If you would like to use humanoid states as you have done, below is a modified version of your code that uses states instead of jump power. It currently triggers off of jumping like ZykoreK’s, but you can change the trigger to whatever you need. I would recommend against InputBegan as your trigger, however, as interacting with the game in any way would disable jumping, including trying to jump (although holding down jump would allow the player to jump after the cooldown has expired).
local plr = game.Players.LocalPlayer
repeat wait() until plr.Character and plr.Character.Parent == game.Workspace
local char = plr.Character or plr.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid")
local on_cooldown = false
function Jump()
if on_cooldown then return end
on_cooldown = true
hum:SetStateEnabled(Enum.HumanoidStateType.Jumping, false)
task.wait(1.2)
hum:SetStateEnabled(Enum.HumanoidStateType.Jumping, true)
on_cooldown = false
end
hum.Jumping:Connect(Jump)
For some reason, neither of the solutions provided are working…? If I hold down space I can still jump multiple times in a row (I’m having this issue on PC)
You are actually only changing its humanoid state, instead of forcing it to jump. Humanoid has a property called “Jump” Setting it to true will cause the character to jump. I guess changing state won’t make it jump.
I’m not trying to make them jump, I’m trying to make a jump cooldown. My code provided works on PC but not on mobile, and the other solutions provided are not working at all.
Omg I’m so stupid… I just realised I put InputBegan (I was testing something), here is my actual code:
InputService = game:GetService("UserInputService")
plr = game.Players.LocalPlayer
repeat wait() until plr.Character and plr.Character.Parent == game.Workspace
char = plr.Character or plr.CharacterAdded:Wait()
hum = char:WaitForChild("Humanoid")
lastJump = 0
function Jump()
local now = tick()
local elapsed = now - lastJump
if elapsed > 1.2 then
lastJump = now
hum:SetStateEnabled(Enum.HumanoidStateType.Jumping, true)
else
hum:SetStateEnabled(Enum.HumanoidStateType.Jumping, false)
end
end
InputService.JumpRequest:Connect(Jump)
Found a solution by myself in case anyone looks at this thread. This works on both mobile and PC, and disables the jump for 1.2 seconds after they jump.
plr = game.Players.LocalPlayer
repeat wait() until plr.Character and plr.Character.Parent == game.Workspace
char = plr.Character or plr.CharacterAdded:Wait()
hum = char:WaitForChild("Humanoid")
debounce = false
cd = 1.2
function Landed(old, new)
if new == Enum.HumanoidStateType.Landed then
hum.JumpPower = 0
wait(cd)
hum.JumpPower = 50
end
end
hum.StateChanged:Connect(Landed)