I made a double jump system in my game, but for some reason whenever you hold the jump button on mobile, the double jump would sometimes activate.
I’ve tried
- Using
:GetPropertyChangedSignal("Jump")instead of jump request. - Making TIME_BETWEEN_JUMPS longer
local UserInputService = game:GetService("UserInputService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Events = ReplicatedStorage:WaitForChild("Events")
local Event = Events:WaitForChild("DoubleJumpEffects")
local Modules = ReplicatedStorage:WaitForChild("Modules")
local GameModules = Modules:WaitForChild("Game")
local Effect = require(GameModules:WaitForChild("DoubleJumpEffects"))
local sound = script.Sound
local doubleJump = {}
local TIME_BETWEEN_JUMPS = 0.1
local DOUBLE_JUMP_POWER_MULTIPLIER = 1.5
local canDoubleJump = false
local hasDoubleJumped = false
local jumpPressed = false
local oldPower = nil
local function requestDoubleJump(humanoid, char)
if not char or not humanoid then
return
end
if humanoid:GetState() == Enum.HumanoidStateType.Dead then
return
end
if canDoubleJump and not hasDoubleJumped then
hasDoubleJumped = true
humanoid.JumpPower = oldPower * DOUBLE_JUMP_POWER_MULTIPLIER
humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
Event:FireServer()
task.spawn(Effect.new, nil, char)
end
end
function doubleJump:Start(char)
if not char then return end
local humanoid = char:WaitForChild("Humanoid")
canDoubleJump = false
hasDoubleJumped = false
oldPower = humanoid.JumpPower
humanoid.StateChanged:Connect(function(_, new)
if new == Enum.HumanoidStateType.Landed then
canDoubleJump = false
hasDoubleJumped = false
humanoid.JumpPower = oldPower
elseif new == Enum.HumanoidStateType.Freefall then
task.wait(TIME_BETWEEN_JUMPS)
if not hasDoubleJumped then
canDoubleJump = true
end
end
end)
humanoid:GetPropertyChangedSignal("Jump"):Connect(function()
if humanoid.Jump then
if not jumpPressed then
jumpPressed = true
requestDoubleJump(humanoid, char)
end
else
jumpPressed = false
end
end)
end
return doubleJump
It works perfectly fine on controller and pc, only mobile is the problem. Any suggestions on how to fix this is appreciated.