Please Fix my Movement Script

So my problem is the jump height is retained for 1 jump after switching from running to walking for example I am walking then I run, and for the first jump while running it will be walking height if possible please add a delay for the debounce to say false so there’s some sort of cooldown
Here is the code:

--Services
local inputService = game:GetService("UserInputService")
local players = game:GetService("Players")

--Variables
local player = players.LocalPlayer
local character = player.Character or player.CharacterAdded:wait()
local normalSpeed = 10
local sprintSpeed =30
local running = false
local humanoid = character:WaitForChild("Humanoid")
local jumpDebounce = true

--Sprint
inputService.InputBegan:Connect(function (key)
	if key.KeyCode == Enum.KeyCode.LeftShift or key.KeyCode == Enum.KeyCode.RightShift then
		running = true
		if humanoid then	
			character.Humanoid.WalkSpeed = sprintSpeed
		end
	end
end)

inputService.InputEnded:Connect(function (key)
	if key.KeyCode == Enum.KeyCode.LeftShift or key.KeyCode == Enum.KeyCode.RightShift then
		running = false
		if humanoid then	
			character.Humanoid.WalkSpeed = normalSpeed
		end
	end
end)

--Disable Jump
inputService.InputBegan:Connect(function(input, gpe)
	if not gpe then
		if input.KeyCode == Enum.KeyCode.Space and jumpDebounce then
			wait(.01)
			jumpDebounce = false
			humanoid.JumpHeight = 0
		end
	end

end)

inputService.InputEnded:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.Space then
		if running  then
			humanoid.JumpHeight = 20
		else
			humanoid.JumpHeight = 10
		end
		jumpDebounce = true
	end
end)
2 Likes
-- Services
local inputService = game:GetService("UserInputService")
local players = game:GetService("Players")

-- Variables
local player = players.LocalPlayer
local character = player.Character or player.CharacterAdded:wait()
local humanoid = character:WaitForChild("Humanoid")

-- Events
local running = false
local jumpDebounce = false

local jumpActive = true

-- Configurations
local configurations = {
	jumpDelay = 1,

	-- walk speed
	defaultSpeed = 10,
	sprintSpeed = 30,

	-- jump height
	sprintJump = 20,
	defaultJump = 10
}

type void = {}; local function activateJump(value: boolean): void
	if value then
		jumpActive = true
		
		if not jumpDebounce then
			humanoid:SetStateEnabled(Enum.HumanoidStateType.Jumping, true)
		end
	else
		task.wait()
		
		jumpActive = false

		if jumpDebounce then
			humanoid:SetStateEnabled(Enum.HumanoidStateType.Jumping, false)
		end
	end
end

-- Sprint
inputService.InputBegan:Connect(function(input)
	if (input.KeyCode == Enum.KeyCode.LeftShift or input.KeyCode == Enum.KeyCode.RightShift) and not running then
		running = true

		if humanoid then
			character.Humanoid.WalkSpeed = configurations.sprintSpeed
		end
		
		-- Desktop / Console
	elseif (input.KeyCode == Enum.KeyCode.Space or input.KeyCode == Enum.KeyCode.ButtonA or input.KeyCode == Enum.KeyCode.ButtonX) and jumpActive then
		activateJump(false)
	end
end)

inputService.InputEnded:Connect(function(input)
	if not inputService:IsKeyDown(Enum.KeyCode.LeftShift) and not inputService:IsKeyDown(Enum.KeyCode.RightShift) and running then -- You had a mistake, if a person held down 2 shift keys and released one, the speed would return to normal. Fixed with IsKeyDown(Key: EnumItem)
		running = false

		if humanoid then	
			character.Humanoid.WalkSpeed = configurations.defaultSpeed
		end
		
		-- Desktop / Console
	elseif (input.KeyCode == Enum.KeyCode.Space or input.KeyCode == Enum.KeyCode.ButtonA or input.KeyCode == Enum.KeyCode.ButtonX) and not jumpActive then
		activateJump(true)
	end
end)

-- Jump
inputService.JumpRequest:Connect(function() -- Jump Request is a method that determines when the player jumps and is triggered before the jump. Your old script was triggered after, and also did not determine whether the player was clicking on the jump from the console/mobile device.
	if jumpActive and not jumpDebounce then
		jumpDebounce = true

		if not jumpActive then
			humanoid:SetStateEnabled(Enum.HumanoidStateType.Jumping, false) -- humanoid:SetStateEnabled(StateType: EnumItem, Value: boolean) allows you to disable/enable the StateType feature
		end

		if running then
			humanoid.JumpHeight = configurations.sprintJump
		else
			humanoid.JumpHeight = configurations.defaultJump
		end

		task.wait(configurations.jumpDelay)

		jumpDebounce = false

		if jumpActive then
			humanoid:SetStateEnabled(Enum.HumanoidStateType.Jumping, true)
		end
	end
end)

-- Mobile
if inputService.TouchEnabled then
	local touchGui = player.PlayerGui:WaitForChild("TouchGui")
	local touchControlFrame = touchGui:WaitForChild("TouchControlFrame")
	local jumpButton = touchControlFrame:WaitForChild("JumpButton")

	jumpButton.InputBegan:Connect(function(input)
		if input.UserInputType == Enum.UserInputType.Touch and jumpActive then
			activateJump(false)
		end
	end)

	jumpButton.InputEnded:Connect(function(input)
		if input.UserInputType == Enum.UserInputType.Touch and not jumpActive then
			activateJump(true)
		end
	end)
end
2 Likes

I’m not sure if you could tell from the code but it was also supposed to disable hold jumping…

Try it now, I’ve updated the code.

Yep, it works like a charm. Thanks!

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