Roblox How To | Creating a Simple Shift to Sprint Script!

Hello! I’m StarJ3M, and I’m going to be teaching you how to make a simple shift to spring ability/key bind! This will be easy to learn and quick!

This is for those people who get free models for Shift To Sprint Scripts.

Step 1 - Making The Script

First of all, you want to make a new local Script inside of StarterPlayerScripts which can be found by going to StarterPlayer, Name it whatever you want!

Step 2 - Coding The Script

Next, we want to open the script. We need to define the character though and the player so that we can put up their walkspeed! to do that you need to put this:

local Players = game:GetService("Players")

local UIS = game:GetService("UserInputService") -- We do this so we can get the players inputs

local player = Players.LocalPlayer -- Gets the player from the local script, Local Player will be considered by its script location., so If it wasn't in a player this would not work

local character = player.CharacterAdded:Wait() -- Waiting until the Player's character gets added to 

local Debounce = false -- a Debounce that you can optionally put.

local CooldownTimer1 = 5

after that, you would want to put:

UIS.InputBegan:Connect(function(input, GPE) -- Connecting to the input that got in, GPE = Game Processed Event and is if the game processed it and if the player is busy, like typing in the chat for an example
	if input.KeyCode == Enum.KeyCode.LeftShift then -- Self Explanitory
		if not Debounce then -- A Debounce if you don't want the player to sprint forever
			Debounce = true
			
			character.Humanoid.WalkSpeed = IntValue -- Set this to any number you want, or variable
			task.wait(10) -- Set the number to anything
			for i = 1, ( Whatever you added to the humanoids Walkspeed ) do -- for loop making it so that the player will slowly stop
				wait(0.5)
				character.Humanoid.WalkSpeed = character.Humanoid.WalkSpeed - i -- Subtracts the current walkspeed from i
			end

            character.Humanoid.WalkSpeed = 16
			
			task.wait(CooldownTimer1) -- Waits the Debounce time for it to be able to use again
			Debounce = false -- Makes it so that it can be used again
		end
	end
end)

And their you go! This is how to make a Simple Shift To Spring Script! Read all of the comments I made if you don’t understand the script!

Please consider liking this post and maybe sharing it to other who need it, It would make me happy considering I put lots of time and hard work into this!


Other Tutorials Related:

Their is Currently No Other Tutorials Related to Scripting.

64 Likes

Hell ye Good job!!!

3 Likes

Nice work! Really descriptive and a useful script!

3 Likes

looks good!
try to disable shift lock in the game so people can use that without accidentally shift locking

2 Likes

This is a good thing for me to note considering that this may annoy people!

2 Likes

Nice tutorial - However I wanna point out the typo in the title, it should be “shift to sprint:slight_smile:

2 Likes

EVERYTIME I WANT TO TYPE SPRINT I ALWAYS PUT SPRING LOL

thanks for telling me, otherwise other people would not understand the title and just walk away

2 Likes

Just one note I’d like to make;
Don’t use “for” “do” loops, tweenService is much more reliable. Otherwise great job this is a great descriptive tutorial!
Keep up the good work :+1:

3 Likes

Thanks for the Info! But how would I use tween service for a players walk speed to go down?

ts:Create(humanoid --[[Tween Info Stuff,]] {WalkSpeed = 0})
3 Likes

almost 20 likes poggers


1 Like

great script I would recomend it to anyone

1 Like
--parent this to 'StarterCharacterScripts'
local tweenService = game:GetService("TweenService")
local humanoid = script.Parent:WaitForChild("Humanoid")

local nextSprint = os.clock()
local origin = humanoid.WalkSpeed

local info = TweenInfo.new(1) -- change '1' to how long you want sprint transitions to take
local target = 24 -- change the number to whatever speed you want to hit
local cooldown = 5 -- change the number to the cooldown you want

local function Sprint()
    tweenService:Create(humanoid, info, {WalkSpeed = target}):Play()
end

local function Unsprint()
    tweenService:Create(humanoid, info, {WalkSpeed = origin}):Play()
end

local function Toggle(_, state)
    if state == Enum.UserInputState.Begin and os.clock() > nextSprint then
         nextSprint = os.clock() + cooldown
          
         return Sprint()
    end

    Unsprint()
end

game:GetService("ContextActionService"):BindAction("ToggleSprint", Toggle, true, Enum.KeyCode.LeftShift, Enum.KeyCode.ButtonL3)

I made it toggleable, among other things

there’s a few other improvements you could make to this, you could make this entire thing a module and allow sprinting to be toggled remotely through exposing those local functions e.g: function module.Sprint, and the ability to sprint as well: module.Start, etc. You could even expose the origin walkspeed so that developers can make the sprinting work with their games that may have a completely dyanmic walkspeed, e.g an orb accessory that increases base walkspeed by 2.

You could also make the tween cancel for a mostly negligible performance differentce (stop tween stacking), but that would make the script a small percent larger, so i didnt bother

9 Likes

Its cool that you made this, but not at all does it really follow the simple factor

Its not very easy to understand.

1 Like

wait() is deprecated and superseded by task.wait() from the task library

2 Likes

Sure, but you should never trade simplicity for functionality. I just rewrote this in a fashion that you may more commonly see in a fully fledged project

Also it’s under 35 lines, that’s pretty simple if you’re going off of size

does everything in scripting have to have more than 50 lines and be complex?

35 lines of code still works out for the script.

1 Like

does everything in scripting have to have more than 50 lines and be complex?

I said:

it’s under 35 lines, that’s pretty simple if you’re going off of size

In that I mean my script can still be considered simple, it’s spaced out and not very long at all.

Functionality should also be considered, regardless of whether it’s aimed to be simple or not, if you’re going to teach someone how to do something, that thing thing should have a practical use, or be rooted in one. I’m not saying the script is bad, however, it is quite unconventional in that most games don’t use that type of sprinting, the user usually expects to be able to toggle their sprinting. There’s also the thing about using wait() to debounce instead of checking if you can sprint right now, doing the former complicates control flow a bit, which isn’t great for beginners.

Anyway, thought i’d make some ratifications to make the script a bit more useful, if you wanna continue, lease dm me, this thread should probably only continue with feedback related to the tutorial lol

1 Like

IM SO MAD AT MYSELF

I LITTERLY PUT “SPRING” IN THE COMMENT

I fixed the typo, you can’t blame me though because the G key is close to the T key B)

2 Likes

From analyzing the code, the script would break after resetting once. Can you confirm?