Fixing Double Jump Script

This is the script for a double jump I have been writing. It works well, but because JumpRequest is called multiple times when jumping off the floor, it glitches out. Is there any way of fixing this?

local MAX_EXTRA_JUMPS = 4 -- 0 = Single jump; 1 = Double jump; etc
local BOOST_MULTI = 3.5
local TIME_BETWEEN_JUMPS = 0.15

local InputService = game:GetService("UserInputService")

local Player = game.Players.LocalPlayer
local Character = Player.Character
local Humanoid = Character:WaitForChild("Humanoid")

local StateType = Enum.HumanoidStateType

local boostCount = 0
local oldHeight = Humanoid.JumpHeight


Humanoid.StateChanged:Connect(function(old, new)
	if new ~= StateType.Landed then return end
	boostCount = 0
end)


function OnJumpRequest()
	print("Jumped")
	
	if Humanoid:GetState() ~= StateType.Freefall then return end
	if boostCount >= MAX_EXTRA_JUMPS then return end

	boostCount += 1
	Humanoid.JumpHeight *= BOOST_MULTI
	wait()
	Humanoid:ChangeState(StateType.Jumping)
	wait()
	Humanoid.JumpHeight /= BOOST_MULTI
	print("Double Jumped")
end

InputService.JumpRequest:Connect(OnJumpRequest)

This is maybe a stupid question but you tried debounce already?

I haven’t tried debounce but I have used a cooldown between each jump. This fixes my problem but jumping off the ground. However, this feels unresponsive as you have to wait 0.2 seconds before you can double jump off the ground.

local MAX_EXTRA_JUMPS = 4 -- 0 = Single jump; 1 = Double jump; etc
local BOOST_MULTI = 5
local TIME_BETWEEN_JUMPS = 0.2

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local InputService = game:GetService("UserInputService")

local Player = game.Players.LocalPlayer
local Character = Player.Character
local Humanoid = Character:WaitForChild("Humanoid")

local StateType = Enum.HumanoidStateType

local boostCount = 0
local oldHeight = Humanoid.JumpHeight
local currentTick = 0
local lastTick = 0


Humanoid.StateChanged:Connect(function(old, new)
	if new ~= StateType.Landed or boostCount == 0 then return end
	
	boostCount = 0
	ReplicatedStorage.BindableEvents.CharacterEvents.SpawnDebris:Fire()
end)


function OnJumpRequest()
	print("Jumped")
	
	-- Cooldown between each jump --
	lastTick = currentTick
	currentTick = tick()
	
	if tick() - lastTick < TIME_BETWEEN_JUMPS then return end
	-- End --
	if Humanoid:GetState() ~= StateType.Freefall then return end
	if boostCount >= MAX_EXTRA_JUMPS then return end

	boostCount += 1
	Humanoid.JumpHeight *= BOOST_MULTI
	wait()
	Humanoid:ChangeState(StateType.Jumping)
	wait()
	Humanoid.JumpHeight /= BOOST_MULTI
	print("Double Jumped")
end

InputService.JumpRequest:Connect(OnJumpRequest)

Oh, you must be referring to that glitch where holding down the space bar causes you to double-jump despite the fact that you didn’t press it twice.

Yes I do mean that. Sorry if I didn’t make it clear enough.

This would be a bit of work, but I suggest adapting this over to use UserInputService, that way you can detect when the spacebar is lifted with InputEnded.

Although this would work better, I want this to work on all platforms.

Managed to fix my problem. I put the new code here: Reliable Customisable Double Jump System

2 Likes