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")
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
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!