Help with shift to sprint

Hey guys! I’m trying to make a shift to spring script and this is my code. It’s not working nor showing any errors. Also it’s in a localscript in starterCharacterScripts. Thanks.

local UserInputService = game:GetService("UserInputService")
local p = game:GetService("Players").LocalPlayer


p.CharacterAdded:Connect(function(c)
	
	local h = c:WaitForChild("Humanoid")
	
	UserInputService.InputBegan:Connect(function(input, processed)
		
		if UserInputService:IsKeyDown(Enum.KeyCode.LeftShift) and not processed then
			
			h.WalkSpeed = 30
			
		else
			
			h.WalkSpeed = 10
			
		end
	end)
end)

local UserInputService = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local c = player.Character

local h = c:WaitForChild("Humanoid")
UserInputService.InputBegan:Connect(function(input, processed)
	while true do
		if UserInputService:IsKeyDown(Enum.KeyCode.LeftShift) then
			h.WalkSpeed = 30
		else
			h.WalkSpeed = 10
		end
		wait(0.01)
	end

end)
1 Like

There you are my good Sir, you cannot use the function of a player being added to the game. You need to do it on a UIS event. If you put the script inside of a characterAdded script, it will only run once. I hope this helps.

It makes it more easy to understand

He can use that. It will do the same.

Yours doesn’t work, I just tried it. I tested mine and it works fine lmao.

Thennnn I try to test mine too :slight_smile: gggggggg

Don’t use this solution, it’s so intensive for no reason.

Use this instead:

local UserInputService = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local c = player.Character or player.CharacterAdded:Wait()

local h = c:WaitForChild("Humanoid")

UserInputService.InputBegan:Connect(function(input, processed)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		if h then
			h.WalkSpeed = 30
		end
	end
end)

UserInputService.InputEnded:Connect(function(input, processed)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		if h then
			h.WalkSpeed = 16
		end
	end
end)

I literally spent a minute on it. If he wanted a better solution he coulda said lmao.

I spent a few minutes on mine as well, they probably didn’t realize how intensive that script was. Just helping them :man_shrugging: