What is a 'Shift to Sprint' script? A shift to Sprint script is a script which allows the players speed to increase when the 'Shift' key is pressed on their keyboard.
The Script So first we want to add a LocalScript into StarterGui
And then we open the script and make a few variables to begin with.
local inputService = game:GetService("UserInputService")
Great! Now we have a variable for the UserInputService! Now let's make another variable!
local inputService = game:GetService("UserInputService")
local plr = game.Players.LocalPlayer
This one is for the player.
local inputService = game:GetService("UserInputService")
local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:wait()
This variable is for the Player's Character. Now let's start making the speed variables for the player.
local inputService = game:GetService("UserInputService")
local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded
local normalSpeed = 16
local sprintSpeed = 25
Awesome! now let's make a variable to see if the player is sprinting or not.
local inputService = game:GetService("UserInputService")
local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded
local normalSpeed = 16
local sprintSpeed = 25
local sprinting = false
Cool! Now we'll start the script To start off we'll see if the input has began, and if it has, connect a function.
inputService.InputBegan:Connect(function(key)
Cool! Now we'll set the variable "Running" which we created earlier to true
inputService.InputBegan:Connect(function(key)
if key.KeyCode == Enum.KeyCode.LeftShift or key.KeyCode == RightShift then
sprinting = true
Awesome! Now let's see if it is the Character, and if it is then change the walkSpeed!
inputService.InputBegan:Connect(function(key)
if key.KeyCode == Enum.KeyCode.LeftShift or key.KeyCode == RightShift then
sprinting = true
if char then
char.Humanoid.WalkSpeed = sprintSpeed
Great! Now we need to make it so that when we let go of 'Shift', we stop sprinting. So type this.
inputService.InputEnded:Connect(function(key)
if key.KeyCode == Enum.KeyCode.LeftShift or key.KeyCode == Enum.KeyCode.RightShift then
running = false
if char then
char.Humanoid.WalkSpeed = normalSpeed
Great! Now Let's put it all together!
local inputService = game:GetService("UserInputService")
local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local normalSpeed = 10
local sprintSpeed = 18
local running = false
inputService.InputBegan:Connect(function (key)
if key.KeyCode == Enum.KeyCode.LeftShift or key.KeyCode == Enum.KeyCode.RightShift then
running = true
if char.Humanoid then
char.Humanoid.WalkSpeed = sprintSpeed
end
end
end)
inputService.InputEnded:Connect(function (key)
if key.KeyCode == Enum.KeyCode.LeftShift or key.KeyCode == Enum.KeyCode.RightShift then
running = false
if char.Humanoid then
char.Humanoid.WalkSpeed = normalSpeed
end
end
end)
Now you should have Shift to Sprint in your ROBLOX game!