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

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?

Any Errors in the Output That you can Find?

This should be player.Character or player.CharacterAdded:Wait()!

I personally find using one script for a sprint script better, I use a module just to wrap simple key down/up actions with UserInputService, and this ends up being my code:

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local KeyInputService = require(ReplicatedStorage.Utils.KeyInputService)

local LocalPlayer = Players.LocalPlayer
local PlayerCharacter = LocalPlayer.Character

LocalPlayer.CharacterAdded:Connect(function(character: Model)
	PlayerCharacter = character
end)

KeyInputService.ConnectToKeyCode(Enum.KeyCode.LeftShift, function(isBegin)
	if PlayerCharacter == nil then
		return
	end

	local humanoid = PlayerCharacter:FindFirstChild("Humanoid")
	if humanoid then
		humanoid.WalkSpeed = isBegin and 22 or 16
	end
end)
4 Likes

Instead of wait, use task.wait(), its more officiant and useful and its more modern and reliable.