[Advanced Tutorial] How to make Basic Sprint Script! (Noob Friendly!)

Hi there,

In this tutorial, i'll be teaching you how to code sprinting system without using TweenService! If my grammar is wrong, I'm so sorry about it because English isn't my main language! 🙇‍♀️

Anyway, Let's start with, Create a script and where it should parent in.

Create A Script!

So in this tutorial, I’ll be create a “LocalScript” and parent in “StarterPlayerScript”
Or you can parent in “StarterCharacterScript”

The LocalScript you can named anything you want to, So i’ll gonna named it “Sprinting”
image


Scripting!

Now here’s the fun part begin, Let’s list all the Services that we need to use!

Determine Services and Local Components and Common Variables!

-- // SERVICES
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")

There you go, Now we got our Services that we need to use in our Script!
Now the next part is, We need to make a Storage for Our Common Variables(Table) and Local Components!

-- // SERVICES
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")

-- // LOCAL
local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character and Character:WaitForChild("Humanoid")

-- // CONST
local CommonVariables = {}

Now you got a Common Variables Storage(Table)! Let’s put our common variables inside of the CommonVariables that we need to use!

-- // CONST
local CommonVariables = {}
CommonVariables.SprintingKeyCode = Enum.KeyCode.LeftShift
CommonVariables.DefaultHumanoidWalkSpeed = 5
CommonVariables.DefaultCameraFieldOfView = 70 -- // OPTIONAL
CommonVariables.CurrentWalkSpeed = CommonVariables.DefaultHumanoidWalkSpeed
CommonVariables.RunningMultipiler = 4
CommonVariables.SprintingAlpha = 0.1

Alright we now got Default Humanoid Walkspeed and Multipiler!

Determine Functions

Let’s create a function that will performing Transition that similar to Tween

-- // FUNCTIONS
local function Lerp(Start, End, Alpha)
	return Start + (End - Start) * Alpha
end

What’s Lerp?

Lerp is stand for “Linear Interpolation”
Lerp is a method of curve fitting in mathematics, Transition from First Number Variable to Another Number Variable with Alpha, Alpha easily understanding is intensity or speed that will change the First Number Variable to Another Number Variable, This might confused you a little bit but it a very useful function that you may find it in almost every freemodel script…

Now let’s add another function where it will checking that LocalPlayer Humanoid Dead State and Local Variables Updater!

local function UpdatedLocalVariables(newCharacter)
	Humanoid = newCharacter:WaitForChild("Humanoid")
	Character = newCharacter
end

local function IsActorDied()
	-- // Actually this function can be shorten,
	-- // But I will not make it way complex and too advanced cuz you might get confusing.
	if Humanoid then
		if Humanoid.Health <= 0 then
			return true
		else
			return false
		end
	else
		warn(("[%s]: Humanoid not found!"):format(script.Name))
		return true
	end
end

This function is used to check that Player is dead or not, by performing get Player Character everytime the function has called and get the Humanoid from Character.

Now let’s make a Updater function where it gonna update the Player WalkSpeed!

local function RenderStepped(Deltatime)
	local ActorDied = IsActorDied()
	
	if not ActorDied and Character then
		if UserInputService:IsKeyDown(CommonVariables.SprintingKeyCode) then
			CommonVariables.CurrentWalkSpeed = Lerp(
				CommonVariables.CurrentWalkSpeed,
				CommonVariables.DefaultHumanoidWalkSpeed * CommonVariables.RunningMultipiler,

				CommonVariables.SprintingAlpha
			)		
		else
			CommonVariables.CurrentWalkSpeed = Lerp(
				CommonVariables.CurrentWalkSpeed,
				CommonVariables.DefaultHumanoidWalkSpeed,

				CommonVariables.SprintingAlpha
			)	
		end
	else
		CommonVariables.CurrentWalkSpeed = Lerp(
			CommonVariables.CurrentWalkSpeed,
			CommonVariables.DefaultHumanoidWalkSpeed,

			CommonVariables.SprintingAlpha
		)	
	end
	
	-- // OPTIONAL
	Camera.FieldOfView = Lerp(
		Camera.FieldOfView,
		(UserInputService:IsKeyDown(CommonVariables.SprintingKeyCode) and CommonVariables.DefaultCameraFieldOfView * 1.25 or CommonVariables.DefaultCameraFieldOfView),
		0.05
	)
	-- // 
	
	Humanoid.WalkSpeed = CommonVariables.CurrentWalkSpeed
end

Great! We got our Updater! The next thing we gonna do is pairs with 2 functions (UpdatedLocalVariables and RenderStepped) to Services!

RunService.RenderStepped:Connect(RenderStepped)
Player.CharacterAdded:Connect(UpdatedLocalVariables)

Final Code!

A-YAYAYAYA!! YOU DID IT! I hope this tutorial will teaching you how to code basic sprinting system without using TweenService

-- // SERVICES
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")

-- // LOCAL
local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character and Character:WaitForChild("Humanoid")

local Camera = workspace.CurrentCamera

-- // CONST
local CommonVariables = {}
CommonVariables.SprintingKeyCode = Enum.KeyCode.LeftShift
CommonVariables.DefaultHumanoidWalkSpeed = 5
CommonVariables.DefaultCameraFieldOfView = 70 -- // OPTIONAL
CommonVariables.CurrentWalkSpeed = CommonVariables.DefaultHumanoidWalkSpeed
CommonVariables.RunningMultipiler = 4
CommonVariables.SprintingAlpha = 0.1

-- // FUNCTIONS
local function UpdatedLocalVariables(newCharacter)
	Humanoid = newCharacter:WaitForChild("Humanoid")
	Character = newCharacter
end

local function Lerp(Start, End, Alpha) : number
	return Start + (End - Start) * Alpha
end

local function IsActorDied()
	if Humanoid then
		if Humanoid.Health <= 0 then
			return true
		else
			return false
		end
	else
		warn(("[%s]: Humanoid not found!"):format(script.Name))
		return true
	end
end

local function RenderStepped(Deltatime)
	local ActorDied = IsActorDied()
	
	if not ActorDied and Character then
		if UserInputService:IsKeyDown(CommonVariables.SprintingKeyCode) then
			CommonVariables.CurrentWalkSpeed = Lerp(
				CommonVariables.CurrentWalkSpeed,
				CommonVariables.DefaultHumanoidWalkSpeed * CommonVariables.RunningMultipiler,

				CommonVariables.SprintingAlpha
			)		
		else
			CommonVariables.CurrentWalkSpeed = Lerp(
				CommonVariables.CurrentWalkSpeed,
				CommonVariables.DefaultHumanoidWalkSpeed,

				CommonVariables.SprintingAlpha
			)	
		end
	else
		CommonVariables.CurrentWalkSpeed = Lerp(
			CommonVariables.CurrentWalkSpeed,
			CommonVariables.DefaultHumanoidWalkSpeed,

			CommonVariables.SprintingAlpha
		)	
	end
	
	-- // OPTIONAL
	Camera.FieldOfView = Lerp(
		Camera.FieldOfView,
		(UserInputService:IsKeyDown(CommonVariables.SprintingKeyCode) and CommonVariables.DefaultCameraFieldOfView * 1.25 or CommonVariables.DefaultCameraFieldOfView),
		0.05
	)
	-- // 

	Humanoid.WalkSpeed = CommonVariables.CurrentWalkSpeed
end

-- // CONNECTIONS
RunService.RenderStepped:Connect(RenderStepped)
Player.CharacterAdded:Connect(UpdatedLocalVariables)

image
If anything went wrong, my apology since i quick made this tutorial, running out of time, But i’ll tried my best to find the time fix the tutorial😿

Editted: I did some changed to the tutorial a little bit and Thanks to @Crygen54 for catching on some messed that i leave in the tutorial, My bad guys.

14 Likes

No offense, but there is 2 wrong things.

At first, you’re referencing the same character and the same humanoid multiple time across the script which is useless and slow down the script rate.

local Character = Player.Character
---
local function IsActorDied()
    local Character = Player.Character
    local Humanoid = Character:WaitForChild("Humanoid")
end)
--
local function RenderStepped(Deltatime)
    local Humanoid = Character:WaitForChild("Humanoid")
end

The second is that the Humanoid.Health can be a decimal number between 0 and 1, (like 0.0307235797) so your check if the humanoid is dead can be wrong sometimes.

if Humanoid.Health < 1 then
	return true
else
	return false
end

Here is the corrected script:

-- // SERVICES
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")

-- // LOCAL
local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character and Character:WaitForChild("Humanoid")

-- // CONST
local CommonVariables = {}
CommonVariables.SprintingKeyCode = Enum.KeyCode.LeftShift
CommonVariables.DefaultHumanoidWalkSpeed = 5
CommonVariables.CurrentWalkSpeed = 5
CommonVariables.RunningMultipiler = 4
CommonVariables.SprintingAlpha = 0.1

-- // FUNCTIONS
local function Lerp(Start, End, Alpha) : number
	return Start + (End - Start) * Alpha
end

local function UpdateInstances(NewCharacter)
	Humanoid = NewCharacter:WaitForChild("Humanoid")
	Character = NewCharacter
end

local function IsActorDied()
	if (Humanoid and Humanoid.Health <= 0) or not Humanoid then
		return true
	else
		return false
	end
end

local function RenderStepped(Deltatime)
	local ActorDied = IsActorDied()

	if not ActorDied and Character then
		if UserInputService:IsKeyDown(CommonVariables.SprintingKeyCode) then
			CommonVariables.CurrentWalkSpeed = Lerp(
				CommonVariables.CurrentWalkSpeed,
				CommonVariables.DefaultHumanoidWalkSpeed * CommonVariables.RunningMultipiler,

				CommonVariables.SprintingAlpha
			)		
		else
			CommonVariables.CurrentWalkSpeed = Lerp(
				CommonVariables.CurrentWalkSpeed,
				CommonVariables.DefaultHumanoidWalkSpeed,

				CommonVariables.SprintingAlpha
			)	
		end
	else
		CommonVariables.CurrentWalkSpeed = Lerp(
			CommonVariables.CurrentWalkSpeed,
			CommonVariables.DefaultHumanoidWalkSpeed,

			CommonVariables.SprintingAlpha
		)	
	end

	Humanoid.WalkSpeed = CommonVariables.CurrentWalkSpeed
end

-- // CONNECTIONS
Player.CharacterAdded:Connect(UpdateInstances)
RunService.RenderStepped:Connect(RenderStepped)
1 Like

Yes, You’re also correct but this tutorial is was pretty rush and trying to make it not way too long line because i was rushing there’s no time on add another function to the update the actor.

I didn’t notice about this one, I might fix by replaced them with the fixed one, Nice catch right there thanks!

1 Like

Yeah that’s fine, your script and tutorial still are great!


I also make this one shorter, but this is optional, in case someone do not want the warn message ^^

local function IsActorDied()
	if (Humanoid and Humanoid.Health <= 0) or not Humanoid then
		return true
	else
		return false
	end
end
1 Like

Could we optimize the sprinting system further by implementing dynamic field of view adjustments based on the player’s speed, rather than relying on a fixed increase when sprinting? and if so would I use renderstep for this?