Greetings Developers,
To get back in context, i’ve made this sprinting system almost 2 years ago while i was a beginner and i’m now updating it to a better version !
Files
Studio: Sprinting.rbxl (151,9 Ko)
Roblox: Sprint System // Professional & Complete - Roblox
Update Changes
Date:15 january 2023
- The MouseLock still is on “LeftControl” keybind (Check medias to change).
- The Local script still is inside the ScreenGui as it is probably easier for beginners to use it.
- The gui bar is now resizing smoother using tween.
- Added camera changes, the FOV is increasing a bit while sprinting can be disabled.
- Local Script got improved and optimized overall.There is no more Values outside the script.
Local Script
--// Services //--
local UserInputService = game:GetService("UserInputService")
local TweenService = game:GetService("TweenService")
local PlayersStorage = game:GetService("Players")
local WorkspaceStorage = game:GetService("Workspace")
--// Variables //--
local Player = PlayersStorage.LocalPlayer
local Character = Player.Character
local Camera = WorkspaceStorage.CurrentCamera
local Humanoid = Character:WaitForChild("Humanoid" ,10)
local Infos = TweenInfo.new(0.25)
local FrameGoal = {}
local CameraGoal = {}
local FrameTween = {}
local CameraTween = {}
--// Settings //--
local CameraChangeEnabled = true
local Keybind = "LeftShift"
local MaxStamina = 100
local NormalWalkspeed = 16
local SprintWalkspeed = 25
local NormalCameraFOV = 70
local SprintCameraFOV = 80
--// Values //--
local Stamina = MaxStamina
local State = false
--// Functions //--
local function BarResize()
FrameGoal.Size = UDim2.fromScale(Stamina / MaxStamina, 1)
FrameTween = TweenService:Create(script.Parent.Bar.Frame, Infos, FrameGoal)
FrameTween:Play()
script.Parent.Bar.Info.Text = Stamina.. "/" ..MaxStamina
end
local function Sprinting()
if State == true and Humanoid and Stamina > 0 then
Humanoid.WalkSpeed = SprintWalkspeed
repeat
wait(0.1)
Stamina -= 1
BarResize()
until
Stamina <= 0 or State == false
Humanoid.WalkSpeed = NormalWalkspeed
end
end
local function Regen()
if State == false and Stamina < MaxStamina then
repeat
wait(0.1)
Stamina += 1
BarResize()
until
Stamina >= MaxStamina or State == true
end
end
local function CameraChange()
if State == true and CameraChangeEnabled == true then
CameraGoal.FieldOfView = SprintCameraFOV
CameraTween = TweenService:Create(Camera, Infos, CameraGoal)
CameraTween:Play()
elseif State == false and CameraChangeEnabled == true then
CameraGoal.FieldOfView = NormalCameraFOV
CameraTween = TweenService:Create(Camera, Infos, CameraGoal)
CameraTween:Play()
end
end
--// Connections //--
UserInputService.InputBegan:Connect(function(key, processed)
if key.UserInputType == Enum.UserInputType.Keyboard then
if key.KeyCode == Enum.KeyCode[Keybind] then
State = true
CameraChange()
Sprinting()
end
end
end)
UserInputService.InputEnded:Connect(function(key, processed)
if key.UserInputType == Enum.UserInputType.Keyboard then
if key.KeyCode == Enum.KeyCode[Keybind] then
State = false
CameraChange()
Regen()
end
end
end)
Medias
Screen Gui placement
Sprint bar (Can be changed)
Change the MouseLock if needed
Hope you will enjoy it !