Run script not working

I’m trying to make a running script.
The problem is, once I press Q, the player’s walkspeed does increase but if I press it again, the player’s walk speed doesn’t change. Basically, if I press Q to run, I can’t start walking again.
Thanks in advance

(There are no errors on the output)

local player = game.Players.LocalPlayer
local char = player.Character
if not char or not char.Parent then
	char = player.CharacterAdded:wait()
end

local UIS = game:GetService("UserInputService")
local running = false
local db = false


UIS.InputBegan:Connect(function(input, gameProcessed)
	if input.KeyCode == Enum.KeyCode.Q then
		if running == false and db == false then
			db = true
			char.Humanoid.WalkSpeed = 32
			wait(3)
			db = false
		elseif running == true and db == false then
			db = true
			char.Humanoid.WalkSpeed = 16
			wait(3)
			db = false
		end
	end
end)

Why do you have 2 debounces? One debounce is fine and will work.

Fixed code:

local player = game.Players.LocalPlayer
local char = player.Character
if not char or not char.Parent then
	char = player.CharacterAdded:wait()
end

local UIS = game:GetService("UserInputService")
local debounce = false


UIS.InputBegan:Connect(function(input, gameProcessed)
	if input.KeyCode == Enum.KeyCode.Q then
		
		if debounce == false then
			char.Humanoid.WalkSpeed = 32
			debounce = true
			return
		end
		
		if debounce == true then
			char.Humanoid.WalkSpeed = 16
			debounce = false
			return
		end
		
	end
end)

(Yes I did test this.)

You never changed the running value after the if statement. So running’s value is always true and thus doesn’t run the code in elseif statement.

Edit: This is the mistake in the code. @KeeganwBLK solution mite b better 4 u.

1 Like

I was trying to use debounce so that you can’t spam the sprint button, if that makes sense.
Anyways, I see my mistake now, thanks for the help!

You can put a wait cooldown for debounce 2 instead of checking.

local player = game.Players.LocalPlayer
local char = player.Character
if not char or not char.Parent then
	char = player.CharacterAdded:wait()
end

local UIS = game:GetService("UserInputService")
local debounce = false
local debounce2 = false


UIS.InputBegan:Connect(function(input, gameProcessed)
	if input.KeyCode == Enum.KeyCode.Q then
		if debounce2 == true then return end
		
		if debounce == false then
			char.Humanoid.WalkSpeed = 32
			debounce2 = true
			wait(3)
			debounce = true
			debounce2 = false
			return
		end
		
		if debounce == true then
			char.Humanoid.WalkSpeed = 16
			debounce2 = true
			wait(3)
			debounce = false
			debounce2 = false
			return
		end
		
	end
end)
1 Like