Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why does this Double Jump fail sometimes?

This double jump script I made works however it sometimes fails to double jump and occasionally breaks completely, it's becoming really annoying and I can't pinpoint where the error is because there's nothing in the output.

I was hoping someone else could help me...Heres the script:

local Player = game.Players.LocalPlayer

while not Player.Character do
	wait()
end

local character = Player.Character
local mouse = Player:GetMouse()
local humanoid = character:WaitForChild("Humanoid")
local torso = character:WaitForChild("Torso")
local head = character:WaitForChild("Head")
local debounce = true

mouse.KeyDown:connect(function(key)
	if key:byte() == 32 then 															
		if humanoid.Jump == true then 												
			if debounce == false then 
				return 
			end
			
			debounce = false
			
			local torso = character:FindFirstChild("Torso")
			torso.Velocity = Vector3.new(torso.Velocity.X, humanoid.JumpPower * 1.7, torso.Velocity.Z)

				
			
		end										
		repeat wait() until humanoid.Jump == false and humanoid:GetState() == Enum.HumanoidStateType.Landed
		
		debounce = true
	end
end)

Thank you for your time.

2 comments

0

repeat wait() until humanoid.Jump == false and humanoid:GetState() == Enum.HumanoidStateType.Landed(source of error) and look at where your end statement for the inner if statement.

0

KeyDown is deprecated. That may be why it won't work.

1 answer

✓ Accepted answer

I'm not entirely sure what is wrong with your code, but here is basically the only improvement that could fix this problem;


UserInputService > KeyDown

KeyDown is deprecated, so it can be removed at any time. It may also not work properly sometimes. Instead, you should use UserInputService.

game:GetService("UserInputService").InputBegan:connect(function(key,gameProcessedEvent)
	if key.KeyCode == Enum.KeyCode.Space then -- it might be SpaceBar or something, I don't have access to KeyCodes at the moment
		-- code
	end
end)

It is very similar to KeyDown, just that the returned key is not a string, but an Object with a property of 'KeyCode', and a second parameter - whether the game processed an event (like when a player is typing, so they don't jump when they press space)


If you're still confused, let me compile the code into your new script:

local Player = game.Players.LocalPlayer

while not Player.Character do
    wait()
end

local character = Player.Character
local mouse = Player:GetMouse()
local humanoid = character:WaitForChild("Humanoid")
local torso = character:WaitForChild("Torso")
local head = character:WaitForChild("Head")
local debounce = true

game:GetService("UserInputService").InputBegan:connect(function(key)
    if key.KeyCode == Enum.KeyCode.Space then                                                            
        if humanoid.Jump == true then                                               
            if debounce == false then 
                return 
            end
            debounce = false
            local torso = character:FindFirstChild("Torso")
            torso.Velocity = Vector3.new(torso.Velocity.X, humanoid.JumpPower * 1.7, torso.Velocity.Z)
        end                                     
        repeat wait() until humanoid.Jump == false and humanoid:GetState() == Enum.HumanoidStateType.Landed
        debounce = true
	end
end)

Also use 'tab' to format your code, not space.

Hope I helped :)

~TDP

1 comment

0

Thanks for the detailed explanation too!

This discussion is preserved as it was. New answers, comments, and votes are closed.