How to make a shift to sprint option

Here is how to make a shift to sprint option for your game.
To Start we need to make the script Make it as Class LocalScript and we will place this script In game.StarterPlayer.StarterPlayerScripts < It is up to you where to put it as long as it works.

First in the script lets define the UserInputService.

-- Variables --
local uis = game:GetService("UserInputService") -- This gives us the power to check what key they are clicking
local isPressing = false -- this will check if they are Holding the shift key or not

Next we will check when the person click a keys

-- Variables --
local uis = game:GetService("UserInputService") -- This gives us the power to check what key they are clicking
local isPressing = false -- this will check if they are Holding the shift key or not

-- Functions --
uis.InputBegan:Connect(function(Input)-- This will check when they start holding the Shift button

end) -- Ends checking InputBegan

Now we want to check if they are clicking the shift button (both Left and Right)

- Variables --
local uis = game:GetService("UserInputService") -- This gives us the power to check what key they are clicking
local isPressing = false -- this will check if they are Holding the shift key or not

-- Functions --
uis.InputBegan:Connect(function(Input)-- This will check when they start holding the Shift button
	
	if Input.KeyCode == Enum.KeyCode.LeftShift or Input.KeyCode == Enum.KeyCode.RightShift then -- This will Check if they are clicking they shift key
		isPressing = true -- They are pressing Shift Key
	end -- ends the if statement
	end) -- Ends checking InputBegan

Now we will finally apply the speed changes when they click shift.

-- Variables --
local uis = game:GetService("UserInputService") -- This gives us the power to check what key they are clicking
local isPressing = false -- this will check if they are Holding the shift key or not

-- Functions --
uis.InputBegan:Connect(function(Input)-- This will check when they start holding the Shift button
	
	if Input.KeyCode == Enum.KeyCode.LeftShift or Input.KeyCode == Enum.KeyCode.RightShift then -- This will Check if they are clicking they shift key
		isPressing = true -- They are pressing Shift Key
	end -- ends the if statement
	
	while true do -- Continues looping through to see if they are clicking right key 
		wait() -- This keeps the game from Crashing
		if isPressing then
			game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 25 -- Increases there speed when they are holding shift
		end-- ends the if
	end -- ends loop
end)-- Ends checking InputBegan

Next we want to check if they have stopped clicking the shift key we do that with InputEnded.
From here everything is basically going to be opposite.

-- Variables --
local uis = game:GetService("UserInputService") -- This gives us the power to check what key they are clicking
local isPressing = false -- this will check if they are Holding the shift key or not

-- Functions --
uis.InputBegan:Connect(function(Input)-- This will check when they start holding the Shift button
	
	if Input.KeyCode == Enum.KeyCode.LeftShift or Input.KeyCode == Enum.KeyCode.RightShift then -- This will Check if they are clicking they shift key
		isPressing = true -- They are pressing Shift Key
	end -- ends the if statement
	
	while true do -- Continues looping through to see if they are clicking right key 
		wait() -- This keeps the game from Crashing
		if isPressing then
			game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 25 -- Increases there speed when they are holding shift
		end-- ends the if
	end -- ends loop
end)-- Ends checking InputBegan

uis.InputEnded:Connect(function(Input) -- This will check when they stop holding the Shift button

end) -- Ends checking InputEnded

Now we want to check if the released the shift key and not another key.

-- Variables --
local uis = game:GetService("UserInputService") -- This gives us the power to check what key they are clicking
local isPressing = false -- this will check if they are Holding the shift key or not

-- Functions --
uis.InputBegan:Connect(function(Input)-- This will check when they start holding the Shift button
	
	if Input.KeyCode == Enum.KeyCode.LeftShift or Input.KeyCode == Enum.KeyCode.RightShift then -- This will Check if they are clicking they shift key
		isPressing = true -- They are pressing Shift Key
	end -- ends the if statement
	
	while true do -- Continues looping through to see if they are clicking right key 
		wait() -- This keeps the game from Crashing
		if isPressing then
			game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 25 -- Increases there speed when they are holding shift
		end-- ends the if
	end -- ends loop
end)-- Ends checking InputBegan

uis.InputEnded:Connect(function(Input) -- This will check when they stop holding the Shift button

	if Input.KeyCode == Enum.KeyCode.LeftShift or Input.KeyCode == Enum.KeyCode.RightShift  then -- This will Check if they stop clicking they shift key
		isPressing = false -- They are not pressing Shift Key
	end-- ends the if statement

end) -- Ends checking InputEnded

Finally we need to make it so they loose that speed when they release shift. To do that we add a while loop.

-- Variables --
local uis = game:GetService("UserInputService") -- This gives us the power to check what key they are clicking
local isPressing = false -- this will check if they are Holding the shift key or not

-- Functions --
uis.InputBegan:Connect(function(Input)-- This will check when they start holding the Shift button
	
	if Input.KeyCode == Enum.KeyCode.LeftShift or Input.KeyCode == Enum.KeyCode.RightShift then -- This will Check if they are clicking they shift key
		isPressing = true -- They are pressing Shift Key
	end -- ends the if statement
	
	while true do -- Continues looping through to see if they are clicking right key 
		wait() -- This keeps the game from Crashing
		if isPressing then
			game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 25 -- Increases there speed when they are holding shift. Can Be Changed to your desire. 
		end-- ends the if
	end -- ends loop
end)-- Ends checking InputBegan

uis.InputEnded:Connect(function(Input) -- This will check when they stop holding the Shift button

	if Input.KeyCode == Enum.KeyCode.LeftShift or Input.KeyCode == Enum.KeyCode.RightShift  then -- This will Check if they stop clicking they shift key
		isPressing = false -- They are not pressing Shift Key
	end-- ends the if statement
	
	while true do  -- Continues looping through to see if they are done clicking the right key 
		wait()-- This keeps the game from Crashing
		if not isPressing then -- Checks if they are done pressing shift button.
			game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 16 -- Decreases there speed when they are holding shift. Default Walk Speed.
		end -- ends the if statement
	end -- ends loop
end) -- Ends checking InputEnded

If you want it or It didn’t work for you go Here its uncopylocked: Shift to sprint Place .

Also Please Like these tutorials take a little bit to make but they are worth it in the end.
Thanks!
lolhi0404

5 Likes

Hello! I’m not a very experienced scripter, but I’ve been testing plugins and other things for a while and found out that the Realism Mod plugin created by @GizmoTjaz has an “shift to run” script. Maybe you can chech that out and compare it to your own script. Here is the plugin link:

https://www.roblox.com/library/400812710/Realism-Mod

Hope it helps!

  • Dvd

Your script changes the walkspeed on the client. Although it works, this is bad practice, as any walkspeed checks on the server (eg. anti-exploit and such) will return invalid results as the value has not changed. I would recommend using RemoteEvents and modifying this code to utilize them.

Edit: Also, why do you have a while true do loop in the input began and input ended functions? This is terrible practice as this sets up a loop every time a button is pressed and un-pressed. This, plus a popular game, is recipe for thousands of while true do loops running at once, thus causing extreme amounts of lag. Consider having one loop outside of these connections. Read what CoolShank8 said below.

2 Likes

There is no reason for a loop here.

Once you set a characters walkspeed, they will walk that speed for example if I set my speed as “20” It will automatically make my speed 20, you don’t have to constantly make the speed 20 that’s just bloat. On top of that you are making a while loop every connection, when an event is me and connected to a function it spawns a new thread, making you have hundereds of threads, like Thundermaker said. Basically you don’t need a loop.

1 Like

Poor programming practices, and not substantial enough for this category.