Issue on double jumping

What do I want to achieve?

I’m trying to make a simple double jump system where you can jump twice and at the 2nd/last jump, you can no longer jump and automatically fall.

Issue?

The jumps are infinite and have no limit. I don’t know how to limit or stop the player from jumping an excessive amount of times.

What did I try to fix this?

I tried to use and mess with hum.StateChanged to control whenever the state of humanoid was changed. I’ve moved around values but it’s all ended to the same conclusion which is an infinite amount of jumps.

Why do I think this is?

I think this is happening because the function is automatically reassigning it’s state to jumping.

Local script in the StarterGui:

local UIS = game:GetService("UserInputService")

local plr = game.Players.LocalPlayer
local hum = plr.Character.Humanoid

local maxjumps = 3
local startjumps = 0

local debounce = false

UIS.InputBegan:Connect(function(input, GPE)
	if input.KeyCode == Enum.KeyCode.Space then
		startjumps = startjumps + 1
		hum:ChangeState(Enum.HumanoidStateType.Jumping)
		print(startjumps)
		if startjumps == maxjumps then
			startjumps = 0
			hum:ChangeState(Enum.HumanoidStateType.Landed)
		end
	end
end)
1 Like

When pressing pressing space bar
startjumps += 1
jump
if max jump
pretend landed and reset startjumps
?

so I assume its just jumping because you dont check if the player actually landed
you just say “landed”

How would I make it so the player can land?

you could try raycasting towards the ground I think that might be best solution? theres probably another way to do that

Instead of having the if statement in the UIS function, have it outside of the function.

hum.StateChanged:Connect(function(old, new)
	if new == Enum.HumanoidStateType.Landed then
		startjumps = 0
	end
end)

Is there a way I can stop the code or jumps from happening though?

In here, add an extra clause

if input.KeyCode == Enum.KeyCode.Space and startjumps <= maxjumps then
1 Like

It works! I never thought also putting the additional if statement there, thanks!

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