Unable to Jump while Sprinting

Hi everyone,

I have run into an issue where the character is unable to jump while sprinting.

This is FULL code:

Summary
local BoostSpeed = 28
local NormalSpeed = 16

RunService.RenderStepped:Connect(function()
	UIS.InputBegan:Connect(function(input, gameProcessed)
		if not gameProcessed then
			if input.KeyCode == Enum.KeyCode.LeftShift then
				humanoid.WalkSpeed = BoostSpeed
				
				if State == "onRunning" then
					UIS.InputEnded:Connect(function(input, gameProcessed)
						if not gameProcessed then
							if input.KeyCode == Enum.KeyCode.Space then
								humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
								wait()
								humanoid.PlatformStand = true
							end
						end
					end)
				end
			end
		end
	end)
	
	UIS.InputEnded:Connect(function(input, gameProcessed)
		if not gameProcessed then
			if input.KeyCode == Enum.KeyCode.LeftShift then
				humanoid.WalkSpeed = NormalSpeed
			end
		end
	end)
end)

As you can see I have already tried to fix this issue by adding this section of code:

                if State == "onRunning" then
					UIS.InputEnded:Connect(function(input, gameProcessed)
						if not gameProcessed then
							if input.KeyCode == Enum.KeyCode.Space then
								humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
								wait()
								humanoid.PlatformStand = true
							end
						end
					end)
				end

I was able fix this issue by just adding the UIS.InputEnded section and the code within it, however, the player was then able to infinitely jump. (Even if you stopped holding Shift, the player can still infinitely jump)
https://gyazo.com/11e4f6c4f278f9c61c4ad33c46fd5c5c

To try and fix that issue, I added the if State == "onRunning", which means that they are on the ground. Once I added, is was basically back to the original state where the character was unable to jump again.

^ I am using a system that made HumanoidStateType basically non-existent, and have a completely remade StateTracker that is working, that’s why I am checking if State. All of my other movement mechanics work, including dashing, double jumping and charged jump, the only issue that I have left is being unable to jump while sprinting.

you are working with multiple nested connections, you can read more about it Here, once you make a connection, it will call that function always it happens, so you dont need to connect everyframe of the game the same event, and once you dont want it more to happen you need to disconnect it

1 Like

There are many ways to solve this, personally I would do it in the following way:

local UIS = game:GetService("UserInputService")
local Players = game:GetService("Players")
local LocalPlayer = Players.LocalPlayer
local character = LocalPlayer.Character or LocalPlayer.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")

local BoostSpeed = 28
local NormalSpeed = 16


UIS.InputBegan:Connect(function(input, gameProcessed)
	if not gameProcessed then
		if input.KeyCode == Enum.KeyCode.LeftShift then
			humanoid.WalkSpeed = BoostSpeed
		end
	end
end)

UIS.InputEnded:Connect(function(input, gameProcessed)
	if not gameProcessed then
		if input.KeyCode == Enum.KeyCode.LeftShift then
			humanoid.WalkSpeed = NormalSpeed
		end
	end
end)

local canJump = true
UIS.InputBegan:Connect(function(input, gameProcessed)
	if not gameProcessed then
		if input.KeyCode == Enum.KeyCode.Space and canJump then
			humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
		end
	end
end)

humanoid.StateChanged:connect(function(old, new)
	if new == Enum.HumanoidStateType.Freefall then
		canJump = false
	elseif new == Enum.HumanoidStateType.Landed then
		canJump = true
	end
end)

By the way, there is no point in using RunService.RenderStepped as the events work all the time.

1 Like

Ahh well this is actually the exact script that I started with. I only added RunService.RenderStepped to check if that may have been an issue.

When I used this code to begin with, I still wasn’t able to jump while sprinting

EDIT:

I actually think that the issue is coming from the ControlModule or one of its child Modules, as the script works as it should in a fresh workplace

1 Like