Is there any way I can upgrade this sprint script?

So I made this sprint LocalScript inside StarterCharacterScripts, anyways I’m trying to make it better than it is, but I don’t know what I missed. Thanks for any help!

image

--// Instances
local ContextActionService = game:GetService("ContextActionService")
local TweenService = game:GetService("TweenService")
local Players = game:GetService("Players")
local LocalPlayer = Players.LocalPlayer
local PlayerGui = LocalPlayer.PlayerGui
local LocalCharacter = LocalPlayer.Character
local LocalHumanoid = LocalCharacter:WaitForChild("Humanoid")
local Camera = workspace.CurrentCamera

local StaminaGUI = PlayerGui:WaitForChild("Stamina")
local FilledStaminaGUI = StaminaGUI:WaitForChild("CanvasGroup"):WaitForChild("Unfilled"):WaitForChild("Filled")

--// Variables
WalkSpeed = 8
RunSpeed = 32
RunFOV = 180
WalkFOV = 70

local CurrentSpeed = WalkSpeed

Stamina = 100
MaxStamina = 100
StaminaRegenStep = 0.5 --// Stamina = MaxStamina / 10; task.wait(1 / StaminaRegenStep)
StaminaLossStep = 4 --// X/Running

local RunTween = nil
local WalkTween = nil

local RunFOVTween = nil
local WalkFOVTween = nil

local RunGUITween = nil
local WalkGUITween = nil

function Walk()
	if RunTween then
		RunTween:Pause()
	end
	if RunFOVTween then
		RunFOVTween:Pause()
	end

	if not WalkTween then
		WalkTween = TweenService:Create(LocalHumanoid, TweenInfo.new(2), {WalkSpeed = WalkSpeed})
	end
	if not WalkFOVTween then
		WalkFOVTween = TweenService:Create(Camera, TweenInfo.new(2), {FieldOfView = WalkFOV})
	end

	WalkTween:Play()

	WalkFOVTween:Play()
end

--// Events
StaminaGUI.Adornee = LocalCharacter

LocalHumanoid.Running:Connect(function(Speed)
	LocalCharacter:WaitForChild("HumanoidRootPart"):WaitForChild("Running").PlaybackSpeed = Speed / 10
	
	CurrentSpeed = Speed
	
end)

LocalHumanoid.WalkSpeed = WalkSpeed

ContextActionService:BindAction("Run", function(actionName, UserInputState, inputObject)
	if UserInputState == Enum.UserInputState.Begin and Stamina >= 1 then
		if WalkTween then
			WalkTween:Pause()
		end
		if WalkFOVTween then
			WalkFOVTween:Pause()
		end
		
		if not RunTween then
			RunTween = TweenService:Create(LocalHumanoid, TweenInfo.new(5), {WalkSpeed = RunSpeed})
		end
		if not RunFOVTween then
			RunFOVTween = TweenService:Create(Camera, TweenInfo.new(5), {FieldOfView = RunFOV})
		end
		
		RunTween:Play()
		
		RunFOVTween:Play()
	elseif UserInputState == Enum.UserInputState.End then
		Walk()
	end
	
end, true, Enum.KeyCode.LeftShift)

ContextActionService:SetTitle("Run", "Run")

while true do
	task.wait()
	if CurrentSpeed >= RunSpeed - WalkSpeed then
		task.wait(1 / StaminaLossStep)
		Stamina = math.max(Stamina - 4, 0)
		
		FilledStaminaGUI.Size = UDim2.fromScale(1, Stamina / MaxStamina)
		
		if WalkGUITween then
			WalkGUITween:Pause()
		end

		if not RunGUITween then
			RunGUITween = TweenService:Create(StaminaGUI:WaitForChild("CanvasGroup"), TweenInfo.new(1), {GroupTransparency = 0})
		end

		RunGUITween:Play()
	elseif CurrentSpeed <= WalkSpeed then
		task.wait(1 / StaminaRegenStep)
		Stamina = math.min(Stamina + MaxStamina / 10, MaxStamina)
		
		FilledStaminaGUI.Size = UDim2.fromScale(1, Stamina / MaxStamina)
		
		if Stamina >= MaxStamina then
			if RunGUITween then
				RunGUITween:Pause()
			end

			if not WalkGUITween then
				WalkGUITween = TweenService:Create(StaminaGUI:WaitForChild("CanvasGroup"), TweenInfo.new(1), {GroupTransparency = 1})
			end
			
			WalkGUITween:Play()

		end
		
	end
	
	--print(Stamina)
	
	if Stamina < 1 then
		Walk()
		
	end
	
end

1 Like

Here are a few suggestions for optimizing and improving the script:

  1. Use local variables as much as possible to reduce the number of global variables and improve performance. For example, you can replace StaminaGUI , FilledStaminaGUI , and LocalCharacter with local variables.
  2. Use the spawn function to run tasks in parallel instead of using multiple while true loops. This will allow you to perform multiple actions at the same time without consuming as much resources.
  3. Use the Enum.KeyCode enum to bind actions to specific keys instead of hardcoding the key names. This will make it easier to change the keybindings and will make the code more readable.
  4. Use the TweenInfo object to specify the duration and easing function for each tween, instead of creating a new TweenInfo object every time. This will improve performance and make the code more readable.
  5. Use the if statement to check if a tween has been created before creating a new one. This will avoid creating unnecessary tweens and will improve performance.
  6. Use the math.max and math.min functions to clamp the value of Stamina between 0 and MaxStamina , instead of using an if statement. This will make the code more concise and easier to read.
3 Likes

Thank you, I already knew and used previously half of the suggestions (specifically all except 2) but I’ll try the new suggestions.

1 Like