Shift To Sprint (With Animation)

In this post I'll go through a decently simple script that implements a sprint mechanic into your game, along with a custom animation!

by Spagheters

Author Avatar

###Setting Up To get started, go ahead and create a LocalScript inside of StarterCharacterScripts. You can call it whatever but I named it 'SprintScript'.


###Variables For this script, I've separated these variables into two groups; Variables and Customizables. Below are the variables, which holds all the values for the player, their character and humanoid, as well as the ContextActionService (CAS) and a Running boolean which we'll use to determine if the player is running.

--\\ VARIABLES //--
local CAS = game:GetService('ContextActionService')
local player = game.Players.LocalPlayer
local Character = workspace:WaitForChild(player.Name)
local Humanoid = Character:WaitForChild('Humanoid')
local Running = false

And below here are customizables. These are variables which you can customize and change depending on your preference. I always tend to capitalise my customizables to let them stand out. You can change these to whatever you want, I've set my player speeds, as well as the keybind to toggle the sprint and the run animation ID (you can add your own)

--\\ CUSTOMIZABLES //--
local RUN_SPEED = 25
local WALK_SPEED = 16
local ANIM_ID = 14662318423
local KEY = "LeftShift"

And finally, let's quickly set up the animation object and allow the humanoid to load it. Here is the code below!

--\\ ANIM SETUP //--
local Animation = Instance.new('Animation')
Animation.AnimationId = 'rbxassetid://' .. ANIM_ID
local LoadAnim = Humanoid:LoadAnimation(Animation)

This step is super simple. I've created a new Animation instance, set the ID to the ID variable that we set, and created a new variable which holds the animation which is now loaded by the player's humanoid.


###Handler Function Now that we've set everything up, let's create the main function that handles whenever a player presses the sprint key. Let's start by putting this line of code at the bottom of the script:

CAS:BindAction('Run', AnimHandler, true, Enum.KeyCode[KEY])

What this code does is links the function we're about to create to the key that we set to toggle sprinting.

Now let's go ahead and create this function:

--\\ FUNCTIONS //--
local function AnimHandler(BindName, InputState)
	if InputState == Enum.UserInputState.Begin and BindName == 'Run' then
		Running = true
		Humanoid.WalkSpeed = RUN_SPEED
	elseif InputState == Enum.UserInputState.End and BindName == 'Run' then
		Running = false
		if LoadAnim.IsPlaying then
			LoadAnim:Stop()
		end
		Humanoid.WalkSpeed = WALK_SPEED
	end
end

The function checks if the player is either beginning to press the key, or is lifting off of it. If the player is pressing down, then our Running variable will be set to true, and the player's speed will be set to running speed. Otherwise if the player is letting go of the sprint key, Running will be set to false, the player will go back to normal speed, and the run animation will be stopped.


###Updating The Humanoid Whilst the player is moving, we need to keep an eye on them and decide whether we need to stop or resume the animation.

--\\ RUNNING UPDATER //--
Humanoid.Running:connect(function(Speed)
	if Speed >= 10 and Running and not LoadAnim.IsPlaying then
		LoadAnim:Play()
		Humanoid.WalkSpeed = RUN_SPEED
	elseif Speed >= 10 and not Running and LoadAnim.IsPlaying then
		LoadAnim:Stop()
		Humanoid.WalkSpeed = WALK_SPEED
	elseif Speed < 10 and LoadAnim.IsPlaying then
		LoadAnim:Stop()
		Humanoid.WalkSpeed = WALK_SPEED
	end
end)

So here it is. When the player is running, we take the speed variable and check:

Humanoid.Changed:connect(function()
	if Humanoid.Jump and LoadAnim.IsPlaying then
		LoadAnim:Stop()
	end
end)

All this code does is checks if the player is jumping and if so, disables the animation. And there you have it, a sprint mechanic with a custom animation along with it! Here is the full final code.

###Full Script

--\\ CUSTOMIZABLES //--
local RUN_SPEED = 25
local WALK_SPEED = 16
local ANIM_ID = 14662318423
local KEY = "LeftShift"

--\\ VARIABLES //--
local CAS = game:GetService('ContextActionService')
local player = game.Players.LocalPlayer
local Character = workspace:WaitForChild(player.Name)
local Humanoid = Character:WaitForChild('Humanoid')
local Running = false

--\\ ANIM SETUP //--
local Animation = Instance.new('Animation')
Animation.AnimationId = 'rbxassetid://' .. ANIM_ID
local LoadAnim = Humanoid:LoadAnimation(Animation)

--\\ FUNCTIONS //--
local function AnimHandler(BindName, InputState)
	if InputState == Enum.UserInputState.Begin and BindName == 'Run' then
		Running = true
		Humanoid.WalkSpeed = RUN_SPEED
	elseif InputState == Enum.UserInputState.End and BindName == 'Run' then
		Running = false
		if LoadAnim.IsPlaying then
			LoadAnim:Stop()
		end
		Humanoid.WalkSpeed = WALK_SPEED
	end
end

--\\ RUNNING UPDATER //--
Humanoid.Running:connect(function(Speed)
	if Speed >= 1 and Running and not LoadAnim.IsPlaying then
		LoadAnim:Play()
		Humanoid.WalkSpeed = RUN_SPEED
	elseif Speed >= 1 and not Running and LoadAnim.IsPlaying then
		LoadAnim:Stop()
		Humanoid.WalkSpeed = WALK_SPEED
	elseif Speed < 1 and LoadAnim.IsPlaying then
		LoadAnim:Stop()
		Humanoid.WalkSpeed = WALK_SPEED
	end
end)

--\\ EVENT //--
CAS:BindAction('Run', AnimHandler, true, Enum.KeyCode[KEY])

Humanoid.Changed:connect(function()
	if Humanoid.Jump and LoadAnim.IsPlaying then
		LoadAnim:Stop()
	end
end)

Thanks for taking the time to read this post (or just yoink the script at the end). If you have any suggestions for improving the script, let me know by dropping a comment, thank you :)

View in-game to comment, award, and more!