Help with Shift To Sprint Script

Hi. I wanted to make a shift to sprint script for my game. I tried to do it myself but realized I should just try a tutorial instead, so I did. The script actually worked, but unfortunately, it didn’t work in the way I intended. Instead of doing shift to sprint, you had to press 0 to sprint instead. I know it says “0” in the code, but I tried changing it to “Shift” or “LeftShift” to no avail. I even replaced the “key == 0” part with an Enum.KeyCode, and it still didn’t work. Can someone please help with this? The code is below:

local playerservice = game:GetService("Players")
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

local sprintSpeed = 30

mouse.KeyDown:Connect(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)

Thanks!

Don’t use GetMouse(), use UserInputService: UserInputService

local playerservice = game:GetService("Players")
local player = game.Players.LocalPlayer
local uis = game:GetService("UserInputService")

local sprintSpeed = 30

uis.InputBegan:Connect(function(key)
	if key.KeyCode == Enum.KeyCode.LeftShift then
		player.Character.Humanoid.WalkSpeed = sprintSpeed
	end
end)

uis.InputEnded:Connect(function(key)
	if key.KeyCode == Enum.KeyCode.LeftShift then
		player.Character.Humanoid.WalkSpeed = 16
	end
end)
1 Like