So, I recently got the motivation and started working on a new game I'm making. I want it to have a sprinting feature, but I can't seem to figure out what is the problem here.
I also followed a tutorial, YouTube link here: https://youtu.be/4z1HVwe0sLE
So I looked in another post in scriptinghelpers, and I found this; https://scriptinghelpers.org/questions/81021/hey-how-do-i-get-character-from-player-in-a-local-script
I followed the answer that someone posted and put it in my variables, but it still wouldn't work.
Here is the code snippet:
UserInputService = game:GetService("UserInputService") local LShiftPressed = false local Player = game.Players.LocalPlayer.Character local Humanoid = Player.Humanoid UserInputService.InputBegan:Connect(function(input, gameProcessedEvent) if input.KeyCode == Enum.KeyCode.LeftShift then LShiftPressed = true while LShiftPressed == true do wait() Humanoid.WalkSpeed = 32 game.Workspace.Sound.Sprint:Play() end end end) UserInputService.InputEnded:Connect(function(input, gameProcessedEvent) if input.KeyCode == Enum.KeyCode.LeftShift then LShiftPressed = false while LShiftPressed == false do wait() Humanoid.WalkSpeed = 16 end end end)
Thank you if you have any feedback. Sorry if this code appears messy.
You're indexing character, so there's that. It could potentially be the Humanoid's name, or that the Sound Instance in workspace is not available or not created.
I would recommend using methods such as Instance.WaitForChild
, Instance.FindFirstChildWhichIsA
to work around for a fix.
UserInputService = game:GetService("UserInputService") local LShiftPressed = false UserInputService.InputBegan:Connect(function(input, gameProcessedEvent) if input.KeyCode == Enum.KeyCode.LeftShift then LShiftPressed = true print(LShiftPressed) while LShiftPressed == true do wait() game.Players.LocalPlayer.Character:WaitForChild("Humanoid").WalkSpeed = 32 game.Workspace.Sound.Sprint:Play() end end end) UserInputService.InputEnded:Connect(function(input, gameProcessedEvent) if input.KeyCode == Enum.KeyCode.LeftShift then LShiftPressed = false print(LShiftPressed) while LShiftPressed == false do wait() game.Players.LocalPlayer.Character:WaitForChild("Humanoid").WalkSpeed = 16 end end end)
if it doesn't work, dm me on discord