Unique sprinting keybinds issue

I’m trying to make the player sprint by pressing W or UP twice, and what happens is that the script fires the cooldown after the sprint begins or ends. Here’s the script:

local UIS = game:GetService("UserInputService")
local debounce1 = true
local debounce2 = true
UIS.InputBegan:Connect(function(key)
	if key.KeyCode == Enum.KeyCode.W or Enum.KeyCode.Up then
		UIS.InputEnded:Connect(function()
			if debounce1 == true and key.KeyCode == Enum.KeyCode.W or Enum.KeyCode.Up then
				debounce1 = false
				if game.Players.LocalPlayer.Character.Humanoid.WalkSpeed == 16 then
					game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 32
				else
					debounce2 = false
					game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 16
					wait(1.5)
					debounce2 = true
				end
				wait(1.5)
				debounce1 = true
			end
		end)
	end
end)

The script is a local script inside of starterplayerscripts.

Hope I can get some help, thanks!

1 Like

Isn’t it the same thing? I see no difference, and it also acts the same as my script.

1 Like

yes but when using the “or” you gotta repeat the word key.KeyCode again

local UIS = game:GetService("UserInputService")
local debounce1 = true
local debounce2 = true
UIS.InputBegan:Connect(function(key)
	if key.KeyCode == Enum.KeyCode.W or Enum.KeyCode.Up then
		local Connection
		Connection = UIS.InputEnded:Connect(function(key)
			if debounce1 == true and key.KeyCode == Enum.KeyCode.W or Enum.KeyCode.Up then
				Connection:Disconnect()
				debounce1 = false
				if game.Players.LocalPlayer.Character.Humanoid.WalkSpeed == 16 then
					game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 32
				else
					debounce2 = false
					game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 16
					wait(1.5)
					debounce2 = true
				end
				wait(1.5)
				debounce1 = true
			end
		end)
	end
end)

this didn’t really change anything though.

Two issues about this script:

The first issue is that the sprint doesn’t begin as soon as you press w or up twice.

The second issue is that the sprint starts by pressing all of the keys.

Maybe try this?

local UserInputService = game:GetService("UserInputService")
local Player = game.Players.LocalPlayer
local TimeInBetween = 1.5
local LastTime = 0
UserInputService.InputBegan:Connect(function(Key)
	if Key.KeyCode == Enum.KeyCode.W or Key.KeyCode == Enum.KeyCode.Up then
		if (os.clock() - LastTime) <= TimeInBetween then
			local Character = workspace:FindFirstChild(Player.Name)
			if Character then
				local Humanoid = Character:FindFirstChild("Humanoid")
				if Humanoid then
					if Humanoid.WalkSpeed == 16 then
						Humanoid.WalkSpeed = 32
					else
						Humanoid.WalkSpeed = 16
					end
					LastTime = 0
				end
			end
		else
			LastTime = os.clock()
		end
	end
end)

This script doesn’t work. It doesn’t really make the player sprint :confused:

Why do you have input began and input ended in one “function”?

Tell me what is outputted using this one:

local UserInputService = game:GetService("UserInputService")
local Player = game.Players.LocalPlayer
local TimeInBetween = 1.5
local LastTime = 0
UserInputService.InputBegan:Connect(function(Key)
	if Key.KeyCode == Enum.KeyCode.W or Key.KeyCode == Enum.KeyCode.Up then
		print("W or Up Key Pressed")
		if (os.clock() - LastTime) <= TimeInBetween then
			print("W or Up Key Pressed Twice")
			local Character = workspace:FindFirstChild(Player.Name)
			if Character then
				print("Character Found")
				local Humanoid = Character:FindFirstChild("Humanoid")
				if Humanoid then
					print("Humanoid Found")
					if Humanoid.WalkSpeed == 16 then
						Humanoid.WalkSpeed = 32
						print("Now Sprinting")
					else
						Humanoid.WalkSpeed = 16
						print("Now Walking")
					end
					LastTime = 0
					print("Key Reset")
				else
					print("Humanoid Not Found")
				end
			else
				print("Character Not Found")
			end
		else
			LastTime = os.clock()
			print("W or Up Key Pressed Once")
		end
	end
end)

Probably best way to accomplish such a thing will be to connect it with BindableEvent.

it printed everything, but it didn’t change my walkspeed. that’s strange.

I just thought of something. Maybe because you are changing the WalkSpeed in a local script? So you may need to connect a remote event and let the server-side change the WalkSpeed.

I’m a bit confused with remote events.

Make sure to add a RemoteEvent in the ReplicatedStorage called RunningRemote.

Your Client Side:

local UserInputService = game:GetService("UserInputService")
local RunningRemote = game.ReplicatedStorage:WaitForChild("RunningRemote")
local Player = game.Players.LocalPlayer
local TimeInBetween = 1.5
local LastTime = 0
UserInputService.InputBegan:Connect(function(Key)
	if Key.KeyCode == Enum.KeyCode.W or Key.KeyCode == Enum.KeyCode.Up then
		if (os.clock() - LastTime) <= TimeInBetween then
			local Character = workspace:FindFirstChild(Player.Name)
			if Character then
				local Humanoid = Character:FindFirstChild("Humanoid")
				if Humanoid then
					if Humanoid.WalkSpeed == 16 then
						Humanoid.WalkSpeed = 32
						RunningRemote:FireServer(32)
					else
						Humanoid.WalkSpeed = 16
						RunningRemote:FireServer(16)
					end
					LastTime = 0
				end
			end
		else
			LastTime = os.clock()
		end
	end
end)

Your Server Side (Add A Regular Script In ServerScriptService):

local RunningRemote = game.ReplicatedStorage:WaitForChild("RunningRemote")
RunningRemote.OnServerEvent:Connect(function(Player,Speed)
	local Character = workspace:FindFirstChild(Player.Name)
	if Character then
		local Humanoid = Character:FindFirstChild("Humanoid")
		if Humanoid then
			Humanoid.WalkSpeed = Speed
		end
	end
end)

It didn’t work sadly :frowning: Maybe the problem comes from something else…

I decide to give up on making the keybind and just use a button for the sprint. Thank you guys for your help though.