Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Sprinting script not working even though I followed an answer on an older post?

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.

2 answers

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.

1 comment

0

I tried WaitForChild() and FindFirstChildWhichIsA() on the "Humanoid" variable but it still didn't work.

#try

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 #Blue Duck#8344

1 comment

0

if it still doesn't work, turn off shiftlock by clicking starterplayer and at the properties find EnableMouseLock and turn it off

This discussion is preserved as it was. New answers, comments, and votes are closed.