You can write your topic however you want, but you need to answer these questions:
-
What do you want to achieve? Keep it simple and clear!
for the sprinting mechanic to stop once exausted, wait, refil, sprint, stop, wait, so on… -
What is the issue? Include screenshots / videos if possible!
exausted mechanic only works once -
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
plenty of looking on devforums
After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!
--{Services}
local userInputService = game:GetService("UserInputService")
local runService = game:GetService("RunService")
-- {{Variables}}
--Player
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local Tweenservice = game:GetService("TweenService")
local camera = workspace.CurrentCamera
--Settings
local stamina = 100
local maxStamina = 100
local drainRate = 50
local refreshRate = 20
-- Booleans
local sprintHeld = false
local sprinting = false
local exhausted = false
-- { functions }
local function sprint(active)
if exhausted then return wait() end
sprinting = active
end
local function onInput(input)
if input.KeyCode == Enum.KeyCode.LeftShift and input.UserInputType ~= Enum.UserInputType.Gamepad1 then
if humanoid.MoveDirection.Magnitude > 0 then
sprintHeld = input.UserInputState == Enum.UserInputState.Begin
sprint(sprintHeld)
end
end
end
local function updateStaminaUI()
script.Parent.Size = UDim2.new(math.clamp(stamina / maxStamina, 0, 1), 0, 1, 0)
end
userInputService.InputBegan:Connect(onInput)
userInputService.InputEnded:Connect(onInput)
runService.Heartbeat:Connect(function(deltaTime)
if sprinting and not exhausted then
stamina = math.max(0, stamina - drainRate * deltaTime)
updateStaminaUI()
humanoid.WalkSpeed = 24
Tweenservice:Create(camera,TweenInfo.new(1,Enum.EasingStyle.Quint,Enum.EasingDirection.Out),{FieldOfView = 90}):Play()
if stamina == 0 then
sprint(false)
exhausted = true
end
else
Tweenservice:Create(camera,TweenInfo.new(1,Enum.EasingStyle.Quint,Enum.EasingDirection.Out),{FieldOfView = 70}):Play()
updateStaminaUI()
humanoid.WalkSpeed = 16
if exhausted == true then
sprint(false)
wait(1)
stamina = math.min(100, stamina + refreshRate * deltaTime)
if stamina >= 10 then
exhausted = false
end
else
stamina = math.min(100, stamina + refreshRate * deltaTime)
exhausted = false
if sprintHeld then
sprint(true)
end
end
end
end)