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!
--// 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