Hello, developers!
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")
We will also create a debouncing variable called isRunning to detect if the Player
is holding down the LeftShift key or not. We talked a little about debouncing in my last tutorial: Beginner Tutorial #2: How To Make A Jumpscare! - Resources / Community Tutorials - Developer Forum | Roblox
local isRunning = false
Step 3: Next, we will use the InputBegan
and InputEnded
events of UIS
and connect those to functions.
The InputBegan event takes two parameters:
- input: InputObject – An InputObject instance, which contains information about the user’s input, such as what key they pressed.
- 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
Make sure isRunning is NOT already true, and if it is, then make it true and set the Humanoid.WalkSpeed
to any desired number. For the sake of the tutorial, though, I will set mine to 32.
if not isRunning then
isRunning = true
Humanoid.WalkSpeed = 32
end
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 released, set isRunning to false and make Humanoid.WalkSpeed
16, by default.
Humanoid.WalkSpeed = 16
isRunning = false
Final code:
local UserInputService = game:GetService("UserInputService")
local Character = script.Parent
local Humanoid = Character:WaitForChild("Humanoid")
local isRunning = false
UserInputService.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.LeftShift then
if not isRunning then
isRunning = true
Humanoid.WalkSpeed = 32
end
end
end)
UserInputService.InputEnded:Connect(function(input)
if input.KeyCode == Enum.KeyCode.LeftShift then
Humanoid.WalkSpeed = 16
isRunning = false
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 this key is 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!