Beginner Tutorial #3: How To Make A Simple Shift To Sprint Script!

Hello, developers! :wave:

Today, I am going to be showing you how to make a simple Shift To Sprint script. I’m sure all of you have seen this system implmented in many games before. I will be talking a little about UserInputService (commonly abbreivated as UIS) and its events InputBegan and InputEnded.

So let’s get started!

Step 1: Create a brand-new LocalScript inside of StarterPlayer < StarterCharacterScripts.

Step 2: We are going to need a couple of variables first. A quick thing I should mention first is that anything parented to StarterCharacterScripts will be added to the player’s character once the game runs. So any scripts inside of this container should be LocalScripts. The first thing we need is to acquire access to UserInputService. We can do this by simply typing the below code:

local UserInputService = game:GetService("UserInputService")

Basically what UserInputService does, is it has events and functions that can detect and capture a user’s input and can also be used to detect interaction on gamepads, computers, and even consoles.
You can read more into it here:
UserInputService | Documentation - Roblox Creator Hub
Now we will get the Character and the Humanoid. Now since this script is inside of StarterCharacterScripts, and we already know what happens to those scripts, we can get the Character by saying:

local Character = script.Parent

Make sense? Now we find the Humanoid.

local Humanoid = Character:WaitForChild("Humanoid")

Step 3: Next, we will use the InputBegan and InputEnded events of UIS and connect those to functions.

The InputBegan event takes two parameters:

  1. input: InputObject – An InputObject instance, which contains information about the user’s input, such as what key they pressed.
  2. gameProcessedEvent: boolean – Basically checks if you are interacting with a CoreGUI, such as the Roblox leaderboard and/or the chat. (We don’t really need this for today, so just ignore it for now.)

We will connect this to an anonymous function and pass through parameter #1.

UserInputService.InputBegan:Connect(function(input)
	
end)

Now we will check if the input detected is the LeftShift key or not. We can do this with an if statement and the Enum.KeyCode array.

if input.KeyCode == Enum.KeyCode.LeftShift then

end

Next, set the Humanoid.WalkSpeed property to any desired number. A good speed to start with is 32. Adjust this as needed.

Humanoid.WalkSpeed = 32	

Now we end the sprint whenever the player lets go of the LeftShift key. We can do this by simply connecting the InputEnded event to another anonymous function. You can pass through the input parameter again and check if the correct key has been released.

UserInputService.InputEnded:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift then

	end
end)

If the LeftShift key has been released, set Humanoid.WalkSpeed to its default value of 16.

Humanoid.WalkSpeed = 16

Final code:

local UserInputService = game:GetService("UserInputService")

local Character = script.Parent
local Humanoid = Character:WaitForChild("Humanoid")

UserInputService.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		Humanoid.WalkSpeed = 32	
	end
end)

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

Step 4: Playtest the game! You should see that upon holding down the LeftShift key, your character will speed up and run. Whenever it’s released, you will slow down and start walking again.

I hope this tutorial helped you and if you have any questions or need help, feel free to ask! If you have any suggestions on what my next tutorial should be, let me know!

Have a wonderful day! :smiley:

53 Likes

New tutorial out now!
Beginner Tutorial #4: How To Make An Animation Play When A Button Is Clicked! - Resources / Community Tutorials - Developer Forum | Roblox

2 Likes

Okay I have a few questions regarding the debounce…
What is the purpose of it? It is useless in this script. I checked it (With the print command just in case, as I am still a beginner in scripting and still learning, which is why I was here) and it serves no purpose here.
Also why are you telling people that the debounce would detect if they Player is holding the LeftShift key or not? The job of a debounce is to stop a script from running again, or running at all until we want it to run (An example would be if you want a cooldown between your light switch being flicked on/off, you would use a debounce, so the players cant spam click it and annoy people or depending on how you make the switch move, make the switch rotate too much)
I came here to learn better about inputs and understand how to detect if someone is holding down a key. While this piece of code helped me in this regard and I appreciate it a lot as an aspiring scripter, I wanted to state that the debounce here is utterly useless and does nothing, it basically just bloats the code. And also the function of a debounce is explained here incorrectly, so to any other aspiring scripters that come by this page I wanted to give a warning.
Read about debounces

1 Like

You are correct, the debounce isnt needed. I’m not sure why he’s included it.

The variable could be used for other systems, which may be why he included it. An example being, a stamina system, or some other system that may need to check if the player is running.

If you were to use the script as is in the tutorial, I would deem it useless though.

1 Like

Thanks for pointing that out!

If I remember correctly, the debounce’s purpose here was so that you couldn’t spam the Shift key (which I don’t think it even did), and if you had another system already implemented, but you and @CloudSnooze are right: it is unnecessary here and adds extra length to the code.

It is removed now and a correct definition of a debounce has been provided.