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â
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)
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.