How come this sprint code is not working?

local UIS = game:GetService("UserInputService")
local char
local players = game.Players

UIS.InputBegan:Connect(function(input, plr)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		plr.Character:FindFirstChild("Humanoid").WalkSpeed = 20
	end
end)

To be clear, this is a LocalScript located in StarterPlayerScripts.
The output gives me an error, saying “Players.pevdn.PlayerScripts.LocalScript:9: attempt to index boolean with ‘Character’”
Any thoughts?

1 Like

Since this is the client, you can use Players.LocalPlayer to get the player. And also, InputBegan’s second parameter is gameProcessedEvent not plr.

local UIS = game:GetService("UserInputService")
local char
local players = game.Players
local plr = players.LocalPlayer

UIS.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		plr.Character:FindFirstChild("Humanoid").WalkSpeed = 20
	end
end)
2 Likes

First, the script should be in StarterCharacterScripts
Second, you should get the character from the localPlayer

local players = game.Players
local player = game.Players.LocalPlayer
local Character = player.Character or player.CharacterAdded:Wait()
UIS.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		Character:FindFirstChild("Humanoid").WalkSpeed = 20
	end
UIS.InputEnded:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		Character:FindFirstChild("Humanoid").WalkSpeed = 16
	end
end)
2 Likes