[Updated] Sprinting System // Stamina, Gui bar, Camera Change

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 ! :grin:


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
Capture

Sprint bar (Can be changed)
Capture2

Change the MouseLock if needed


Hope you will enjoy it ! :heartpulse:

55 Likes

Having Stamina as an integer value is easily exploitable; Just thought you might wanna know.

1 Like

Yep that’s right, but making it in server is going to take too much ressources for the server and decrease game performances.
So you need to choose between a exploitable system or a laggy game.

4 Likes

It’s actually quite difficult as an exploiter to modify local values. So I’d suggest making something like:

local Stamina = 100

If an exploiter tried to access that, it would look like:

local v1 = 100

If my previous experience is correct.

4 Likes

you might want to explain what the code do, so when a beginner look at your codeblock they roughly understand the concept of it rather than copy and paste and not learning anything.

Exploiter cant change a variable data inside script

1 Like

I did this however the stamina bar goes out of frame and Im unsure how to fix it:
image

or you can just have

local Stamina = 100

instead of

local Stamina = script.IntValue.Value

Not exploitable and not laggy

1 Like

The value inside the script is a bool, to check sprint activation, so don’t care if exploiter are changing it, it will just active or desactive the sprint.

it’s still better to use a local variable because values like shown in the script are still exploitable

Yep, that’s true.
Also this ressource is old, i made it long time ago and i still was a beginner ^^

As it is comming back, i’m probably going to update it

Updated it, let me know if i need to change something or if you got a question, thanks.

Yes, they can.
They can change anything inside any local script because their client has access to the code.

You still can store the “MaxStamina” and “Stamina” as player values in server side , then add a RemoteEvent:FireServer() inside the “Sprinting()” and “Regen” functions (do the repeat code inside the server side script with “onserverevent”) and run the BarResize with Stamina.Changed so the most important stats can’t be exploited.

local function Sprinting()
	RemoteEvent:FireServer()
end

local function Regen()
	RemoteEvent:FireServer()
end

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

Player.Stamina.Changed:Connect(function()
    BarResize()
end)
2 Likes

Yes but only read, not change inside it but they can write a similiar code too

No, they can change it too.
Client code is stored in RAM, so script executors can simply access it and edit properties.

An issue the stamina and FOV change when I press Shift even when I’m not moving

Use CanvasGroups, Any of it’s descendants can’t go out of the frame, read the documentation if you don’t know anything about CanvasGroups

1 Like