Shift to sprint script not working

I’m making a shift-to-sprint script, and it does nothing.
Script:

local player = game.Players.LocalPlayer
local mouse = player.GetMouse()
local sprintspeed = 40
mouse.KeyDown(function(key)
if key == "0" then
player.Character.Humanoid.WalkSpeed = sprintspeed
end
end)
mouse.KeyUp:Connect(function(key)
if key == "0" then
player.Character.Humanoid.WalkSpeed = 16
end
end)
1 Like

Why would you want to be the key “0” numkey to be the sprint button, i think you should change it to left shift or any shift buttons so it’s more easier.

I would suggest using the UserInputService rather than KeyDown

Anyways, there’s a typo there so you should change the period to : instead.

How do I put it as shift, as it isn’t a character?

User input service is far better than that since it uses all keys like the keyboard and the mouse.

Alright. I’ll test it out, and see how it works.

game:GetService("UserInputService").InputBegan:Connect(function(input, gameProcessed)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		local Character = game.Players.LocalPlayer.Character
		if Character ~= nil then
			local Humanoid = Character:FindFirstChild("Humanoid")
			if Humanoid ~= nil then
				Humanoid.WalkSpeed = 40
			end
		end
	end
end)
game:GetService("UserInputService").InputEnded:Connect(function(input, gameProcessed)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		local Character = game.Players.LocalPlayer.Character
		if Character ~= nil then
			local Humanoid = Character:FindFirstChild("Humanoid")
			if Humanoid ~= nil then
				Humanoid.WalkSpeed = 16
			end
		end
	end
end)

I recommend you to use

Here is a working example of your script

-- [[Service Calling]] --

local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")

-- [[Player Content]] --

local LocalPlayer = Players.LocalPlayer or Players.PlayerAdded:Wait()
local Character = LocalPlayer.Character or LocalPlayer.CharacterAdded:Wait() or workspace:FindFirstChild(LocalPlayer.Name)
local Humanoid = Character:WaitForChild("Humanoid")

-- [[Basics]] --

local SprintSpeed = 40 -- Sprint speed
local DefaultSpeed = 16 -- Default one

-- [[Start Of Script]] --

UserInputService.InputBegan:Connect(function(Init,Deny)
	if UserInputService.KeyboardEnabled and not Deny and Init.KeyCode == Enum.KeyCode.LeftShift or Init.KeyCode == Enum.KeyCode.RightShift then
		if Humanoid then
			Humanoid.WalkSpeed = SprintSpeed
		end
	end 
end)

UserInputService.InputEnded:Connect(function(Init,Deny)
	if UserInputService.KeyboardEnabled and not Deny and Init.KeyCode == Enum.KeyCode.LeftShift or Init.KeyCode == Enum.KeyCode.RightShift then
		if Humanoid then
			Humanoid.WalkSpeed = DefaultSpeed
		end
	end 
end)

I hope this helps you!

Is this in a local or server script?

Guess

That is why I am asking if it is or not lol

It is a LocalScript as LocalPlayer is only accessible on the client.

That is why I am asking if it is, because if it is not that will be a problem.

I just answered your question?

Ohh, I was just making sure it was in a local script because it could be one of the reasons the script wont work.

if key.KeyCode == Enum.KeyCode.0 then*

The key argument is a string with mouse.KeyDown