I need help with this script

  1. I want to achieve a double jump system where when the play jumps twice it does it twice

  2. the issue is when the player hold the jump button or space bar it doubles

  3. i have tried to fix it

why does it double jump when you hold jump here is some of the script

jumpEvent.Event:Connect(function()
    if onGround == false then
        script.Parent.Enabled = true
    end
    
    if jumpsLeft > 0 and char and debounce == false then
        if onGround == false then
            debounce = true
            jumpsLeft -= 1
            label.Text = jumpsLeft.."/"..maxJumps
            char.Humanoid.JumpPower = 50
            
            bar:TweenSize(UDim2.new(jumpsLeft / maxJumps,0,1,0),
                Enum.EasingDirection.InOut,
                Enum.EasingStyle.Quad,
                .1
            )
            if jumpsLeft == 0 then
                delay(.1, function()
                    bar.Visible = false
                end)
            end
            char.Humanoid.JumpPower = 150
            anim:Play()
            char.Humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
            delay(0.3, function()
                debounce = false
            end)
        end
    end
end)

userInput.JumpRequest:Connect(function()
    jumpEvent:Fire()
end)

Please don’t write the full script

Thank you

1 Like

The documentation actually mentions this: UserInputService.JumpRequest (roblox.com). Looks like that page recommends using a debounce to fix it.

EDIT:
It looks like you set up a boolean to handle this yourself. If you want to do it this way, make sure you define the variable “debounce” outside of the event.

it still doesnt work it is doing the same thing

Would you mind posting the full script, with your edits?

ok

local userInput = game:GetService("UserInputService")

local player = game.Players.LocalPlayer

repeat wait() until player.Character

local char = player.Character
player.CharacterAdded:Connect(function(c)
	char = c
end)

local jumpEvent = script.Parent.Jumped
local animation = script:WaitForChild("Boost")
local anim = char.Humanoid:LoadAnimation(animation)
local upgradeEvent = script.Parent.Upgrade

local background = script.Parent.Background
local bar = background.Bar
local label = script.Parent.Label

local maxJumps = player.DJumps.MaxDJumps.Value
local jumpsLeft = maxJumps

local onGround = false
local debounce = false

coroutine.resume(coroutine.create(function()
	while wait(.1) do
		local ray = Ray.new(char.HumanoidRootPart.Position, Vector3.new(0,-3,0))
		local hit,pos = workspace:FindPartOnRayWithIgnoreList(ray,{char})
		
		if hit then
			onGround = true
			
			bar:TweenSize(UDim2.new(1,0,1,0),
				Enum.EasingDirection.InOut,
				Enum.EasingStyle.Quad,
				.1
			)
			
			maxJumps = player.DJumps.MaxDJumps.Value
			char.Humanoid.JumpPower = 50
			script.Parent.Enabled = false
			bar.Visible = true
			jumpsLeft = maxJumps
			label.Text = jumpsLeft.."/"..maxJumps
		else
			onGround = false
		end
	end
end))

upgradeEvent.Event:Connect(function(newValue)
	maxJumps = newValue
end)

local db = 0

if db == 2 then
	wait(3)
	db = 0
else
	jumpEvent.Event:Connect(function()
		if onGround == false then
			script.Parent.Enabled = true
		end

		if jumpsLeft > 0 and char and debounce == false then
			db = db + 1
			if onGround == false then
				debounce = true
				jumpsLeft -= 1
				label.Text = jumpsLeft.."/"..maxJumps
				char.Humanoid.JumpPower = 50

				bar:TweenSize(UDim2.new(jumpsLeft / maxJumps,0,1,0),
					Enum.EasingDirection.InOut,
					Enum.EasingStyle.Quad,
					.1
				)
				if jumpsLeft == 0 then
					delay(.1, function()
						bar.Visible = false
					end)
				end
				char.Humanoid.JumpPower = 150
				anim:Play()
				char.Humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
				delay(0.3, function()
				end)
			end
		end
	end)

	userInput.JumpRequest:Connect(function()
		jumpEvent:Fire()
	end)
	end

the problem is now when the player loads in it works and then when the player try to double it doesn’t work

Well you are not setting “debounce” back to false from what I can see. It looks like you removed that in the updated script. If it still double jumps by holding jump, would you mind placing a print(debounce) inside the event, just to double check?

Here is the script before i changed it

local userInput = game:GetService("UserInputService")

local player = game.Players.LocalPlayer

repeat wait() until player.Character

local char = player.Character
player.CharacterAdded:Connect(function(c)
	char = c
end)

local jumpEvent = script.Parent.Jumped
local animation = script:WaitForChild("Boost")
local anim = char.Humanoid:LoadAnimation(animation)
local upgradeEvent = script.Parent.Upgrade

local background = script.Parent.Background
local bar = background.Bar
local label = script.Parent.Label

local maxJumps = player.DJumps.MaxDJumps.Value
local jumpsLeft = maxJumps

local onGround = false
local debounce = false

coroutine.resume(coroutine.create(function()
	while wait(.1) do
		local ray = Ray.new(char.HumanoidRootPart.Position, Vector3.new(0,-3,0))
		local hit,pos = workspace:FindPartOnRayWithIgnoreList(ray,{char})
		
		if hit then
			onGround = true
			
			bar:TweenSize(UDim2.new(1,0,1,0),
				Enum.EasingDirection.InOut,
				Enum.EasingStyle.Quad,
				.1
			)
			
			maxJumps = player.DJumps.MaxDJumps.Value
			char.Humanoid.JumpPower = 50
			script.Parent.Enabled = false
			bar.Visible = true
			jumpsLeft = maxJumps
			label.Text = jumpsLeft.."/"..maxJumps
		else
			onGround = false
		end
	end
end))

upgradeEvent.Event:Connect(function(newValue)
	maxJumps = newValue
end)

jumpEvent.Event:Connect(function()
	if onGround == false then
		script.Parent.Enabled = true
	end
	
	if jumpsLeft > 0 and char and debounce == false then
		if onGround == false then
			debounce = true
			jumpsLeft -= 1
			label.Text = jumpsLeft.."/"..maxJumps
			char.Humanoid.JumpPower = 50
			
			bar:TweenSize(UDim2.new(jumpsLeft / maxJumps,0,1,0),
				Enum.EasingDirection.InOut,
				Enum.EasingStyle.Quad,
				.1
			)
			if jumpsLeft == 0 then
				delay(.1, function()
					bar.Visible = false
				end)
			end
			char.Humanoid.JumpPower = 150
			anim:Play()
			char.Humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
			delay(0.3, function()
				debounce = false
			end)
		end
	end
end)

userInput.JumpRequest:Connect(function()
	jumpEvent:Fire()
end)

When you hold down jump, does it instantly double jump, or does it wait 0.3 seconds before double jumping?

do you want to play the game? and see what happens

Boxing Champions (BETA) - Roblox

This LocalScript works.
https://www.roblox.com/library/3241662825/Doble-jump

Try increasing the time before setting “debounce” to false. Let me know if that fixes it.

how should I do that please could you tell me

Change

delay(0.3, function()
   debounce = false
end)

to

delay(1, function()
	debounce = false
end)

does work still when I hold the jump button it does a double

Does it wait for one second before doing the double jump, or does it immediately happen?

no (i am just doing this because i have to write 30 words)

Alright. If you place a print(debounce) inside of your event, what does it return? You also may want to check for the state change to falling, and set “debounce” to false when that occurs, instead of waiting for a set period of time: Humanoid.StateChanged (roblox.com).

EDIT:
You may be having an issue because of the script detecting when a player is on the ground. Try changing the Vector3 to Vector3.new(0, -4, 0). Add a wait(0.5) before onGround = false. Let me know if this fixes it.

I know that you did not want a script given to you, but if it is still not working you might cross reference your version of the script, with this one inside of StarterCharacterScripts (which works for me on a baseplate):

local userInput = game:GetService("UserInputService")

local player = game.Players.LocalPlayer


local char = script.Parent

local maxJumps = 1
local jumpsLeft = maxJumps

local onGround = true
local debounce = false

char.Humanoid.StateChanged:connect(function(Old, New)
	if New == Enum.HumanoidStateType.Landed then
		onGround = true
		char.Humanoid.JumpPower = 50
		jumpsLeft = maxJumps
	elseif (New == Enum.HumanoidStateType.Jumping) or (New == Enum.HumanoidStateType.Freefall) then
		print("Changed")
		wait(0.3)
		onGround = false
	end
end)



function jumpEvent()
	if onGround == false then
	else
	end

	if (jumpsLeft > 0) and (char) and (debounce == false) then
		print("JumpsLeft:"..jumpsLeft.." debounce:"..tostring(debounce))
		if onGround == false then
			debounce = true
			jumpsLeft -= 1
			char.Humanoid.JumpPower = 150


			char.Humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
			print("DOUBLE")
			wait(0.3)
			print(debounce)
			debounce = false
		end
	end
end

userInput.JumpRequest:Connect(function()
	jumpEvent()
end)

This worked thank you this has really helped

1 Like