In short
Put a LocalScript in StarterPlayerScripts. Keep a stamina number from 0 to 100. Every frame in RunService.Heartbeat, take away DRAIN_PER_SECOND * deltaTime while Shift is held and the player is moving, and add REGEN_PER_SECOND * deltaTime once they have rested for a second. Set the width of a Frame in a ScreenGui to stamina / 100 to draw the bar.
- Multiply by
deltaTimeso the bar lasts the same number of seconds at 30 or 240 frames per second. - When the bar hits 0, block sprinting until it is back above a minimum such as 30. Without that, an empty bar flickers between sprinting and walking.
- Stamina lives on the player's device, like sprinting itself. A cheater can ignore it, so never let it guard anything valuable.
Get the script
Stamina bar script with sprint and GUI
One LocalScript in StarterPlayerScripts: hold Shift to sprint while a stamina bar drains, rest to refill it, and the bar turns red when low.
-- LocalScript in StarterPlayerScripts named StaminaSprint.
-- Hold Shift (L3 on a gamepad, or the Sprint button on a phone) to sprint while stamina lasts.
local ContextActionService = game:GetService("ContextActionService")
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local MAX_STAMINA = 100
local SPRINT_BONUS = 10 -- added to WalkSpeed while sprinting (16 becomes 26)
local DRAIN_PER_SECOND = 20 -- a full bar lasts MAX_STAMINA / DRAIN_PER_SECOND seconds
local REGEN_PER_SECOND = 15
local REGEN_DELAY = 1 -- seconds of rest before stamina refills
local MIN_TO_SPRINT = 30 -- after running dry, stamina needed to sprint again
local JUMP_COST = 0 -- stamina per jump, 0 for free jumps
local player = Players.LocalPlayer
local stamina = MAX_STAMINA
local sprintHeld = false
local sprinting = false
local tired = false -- true after the bar hits 0, until it is back to MIN_TO_SPRINT
local normalSpeed = 16
local lastUse = 0
local jumpAllowed = true
-- The bar: a dark frame with a coloured fill and a number on top
local gui = Instance.new("ScreenGui")
gui.Name = "StaminaGui"
gui.ResetOnSpawn = false -- keep the bar through respawns
local bar = Instance.new("Frame")
bar.AnchorPoint = Vector2.new(0.5, 1)
bar.Position = UDim2.new(0.5, 0, 1, -28)
bar.Size = UDim2.fromOffset(260, 16)
bar.BackgroundColor3 = Color3.fromRGB(24, 22, 32)
bar.Parent = gui
local stroke = Instance.new("UIStroke")
stroke.Color = Color3.fromRGB(255, 255, 255)
stroke.Transparency = 0.7
stroke.Thickness = 1.5
stroke.Parent = bar
local fill = Instance.new("Frame")
fill.Size = UDim2.fromScale(1, 1)
fill.BorderSizePixel = 0
fill.Parent = bar
for _, frame in { bar, fill } do
local corner = Instance.new("UICorner")
corner.CornerRadius = UDim.new(1, 0)
corner.Parent = frame
end
local label = Instance.new("TextLabel")
label.BackgroundTransparency = 1
label.Size = UDim2.fromScale(1, 1)
label.Font = Enum.Font.GothamBold
label.TextSize = 12
label.TextColor3 = Color3.fromRGB(255, 255, 255)
label.TextStrokeTransparency = 0.6
label.ZIndex = 2
label.Parent = bar
gui.Parent = player:WaitForChild("PlayerGui")
local FULL_COLOR = Color3.fromRGB(80, 220, 140)
local LOW_COLOR = Color3.fromRGB(240, 70, 70)
local TIRED_COLOR = Color3.fromRGB(120, 120, 130)
local function getHumanoid(): Humanoid?
local character = player.Character
local humanoid = character and character:FindFirstChildOfClass("Humanoid")
if humanoid and humanoid.Health > 0 then
return humanoid
end
return nil
end
local function drawBar()
local share = stamina / MAX_STAMINA
fill.Size = UDim2.fromScale(share, 1)
-- Green when full, red when empty, grey while tired
fill.BackgroundColor3 = if tired then TIRED_COLOR else LOW_COLOR:Lerp(FULL_COLOR, share)
label.Text = tostring(math.floor(stamina))
end
local function onSprint(_name: string, state: Enum.UserInputState, _input: InputObject): Enum.ContextActionResult
if state == Enum.UserInputState.Begin then
sprintHeld = true
elseif state == Enum.UserInputState.End or state == Enum.UserInputState.Cancel then
sprintHeld = false
end
return Enum.ContextActionResult.Pass
end
ContextActionService:BindAction("StaminaSprint", onSprint, true, Enum.KeyCode.LeftShift, Enum.KeyCode.ButtonL3)
ContextActionService:SetTitle("StaminaSprint", "Sprint")
RunService.Heartbeat:Connect(function(deltaTime: number)
local humanoid = getHumanoid()
if not humanoid then
return
end
local now = os.clock()
local moving = humanoid.MoveDirection.Magnitude > 0
local shouldSprint = sprintHeld and moving and not tired and stamina > 0
-- Change WalkSpeed only when sprinting starts or stops, so speed from other scripts is kept
if shouldSprint and not sprinting then
sprinting = true
normalSpeed = humanoid.WalkSpeed
humanoid.WalkSpeed = normalSpeed + SPRINT_BONUS
elseif not shouldSprint and sprinting then
sprinting = false
humanoid.WalkSpeed = normalSpeed
end
if sprinting then
stamina = math.max(0, stamina - DRAIN_PER_SECOND * deltaTime)
lastUse = now
if stamina == 0 then
tired = true
end
elseif now - lastUse >= REGEN_DELAY then
stamina = math.min(MAX_STAMINA, stamina + REGEN_PER_SECOND * deltaTime)
end
if tired and stamina >= MIN_TO_SPRINT then
tired = false
end
-- Jumping: allowed only with enough stamina (SetStateEnabled works on this device only)
if JUMP_COST > 0 then
local canJump = stamina >= JUMP_COST
if canJump ~= jumpAllowed then
jumpAllowed = canJump
humanoid:SetStateEnabled(Enum.HumanoidStateType.Jumping, canJump)
end
end
player:SetAttribute("Stamina", stamina)
drawBar()
end)
-- Each new character: full bar, jumping allowed, and jumps cost stamina
local function onCharacter(character: Model)
stamina = MAX_STAMINA
sprintHeld = false
sprinting = false
tired = false
jumpAllowed = true
local humanoid = character:WaitForChild("Humanoid") :: Humanoid
humanoid.StateChanged:Connect(function(_old: Enum.HumanoidStateType, new: Enum.HumanoidStateType)
if new == Enum.HumanoidStateType.Jumping and JUMP_COST > 0 then
stamina = math.max(0, stamina - JUMP_COST)
lastUse = os.clock()
end
end)
end
player.CharacterAdded:Connect(onCharacter)
if player.Character then
task.spawn(onCharacter, player.Character)
end
drawBar()
RoCode builds it in the place you have open in Roblox Studio, on Windows or macOS with a desktop browser; on a phone the button sends the prompt and the link to your computer. Free to start, with free credits every day. More Roblox Studio scripts
Where to put it
StarterPlayer
StarterPlayerScripts
StaminaSprint LocalScript new
Settings you can change
DRAIN_PER_SECONDIn the script- Stamina used per second of sprinting. At 20, a full bar of 100 lasts 5 seconds. Default:
20. REGEN_PER_SECONDIn the script- Stamina gained per second while not sprinting, after the rest delay. Default:
15. JUMP_COSTIn the script- Stamina each jump costs. 0 means jumping is free; 10 makes every jump use a tenth of the bar. Default:
0.
If you already have a sprint script, such as ShiftToSprint from the sprint guide, delete it first: both bind Left Shift and both change WalkSpeed.
What a cheater can and cannot doEach player's device moves its own character, so a cheater can sprint forever whatever this script says. The server can only measure how far characters move: the sprint guide's movement check does that.
Type-checked with luau-lsp strict against Roblox's API definitions on 14 November 2026. Not playtested in Studio yet: press Play and watch the Output window before you publish.
Prompt for RoCode
Add stamina to my game: one LocalScript in StarterPlayerScripts that sprints at WalkSpeed plus 10 while Left Shift, L3 or a phone button is held, drains 20 stamina per second while sprinting and moving, refills 15 per second after 1 second of rest, stops sprinting at 0 until stamina is back to 30, and draws a rounded stamina bar at the bottom of the screen that goes from green to red as it empties.
On this page
How stamina works
Sprinting in Roblox means raising the Humanoid's WalkSpeed, which is 16 by default. Stamina is a rule on top of that: you may only sprint while a number is above zero. The number goes down while you sprint and back up when you stop. If you only need the sprint without stamina, the shift to sprint guide has a shorter script.
| What the player does | What happens to stamina |
|---|---|
| Holds Shift and moves | Drains 20 per second. WalkSpeed goes up by 10. |
| Holds Shift but stands still | Nothing drains. There is no sprint while standing. |
| Stops sprinting | Refills 15 per second after 1 second of rest. |
| Runs the bar to 0 | Tired: no sprint until stamina is back to 30. The bar turns red. |
| Jumps (if JUMP_COST is above 0) | Each jump costs stamina. No jump when there is not enough. |
Step 1: The stamina LocalScript
- In the Explorer, expand StarterPlayer, hover over StarterPlayerScripts, click + and insert a LocalScript. Name it
StaminaSprint. - Replace its contents with the code below.
- Press Play, hold Left Shift and walk. The bar at the bottom of the screen drains.
-- LocalScript in StarterPlayerScripts named StaminaSprint.
-- Hold Shift (L3 on a gamepad, or the Sprint button on a phone) to sprint while stamina lasts.
local ContextActionService = game:GetService("ContextActionService")
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local MAX_STAMINA = 100
local SPRINT_BONUS = 10 -- added to WalkSpeed while sprinting (16 becomes 26)
local DRAIN_PER_SECOND = 20 -- a full bar lasts MAX_STAMINA / DRAIN_PER_SECOND seconds
local REGEN_PER_SECOND = 15
local REGEN_DELAY = 1 -- seconds of rest before stamina refills
local MIN_TO_SPRINT = 30 -- after running dry, stamina needed to sprint again
local JUMP_COST = 0 -- stamina per jump, 0 for free jumps
local player = Players.LocalPlayer
local stamina = MAX_STAMINA
local sprintHeld = false
local sprinting = false
local tired = false -- true after the bar hits 0, until it is back to MIN_TO_SPRINT
local normalSpeed = 16
local lastUse = 0
local jumpAllowed = true
-- The bar: a dark frame with a coloured fill and a number on top
local gui = Instance.new("ScreenGui")
gui.Name = "StaminaGui"
gui.ResetOnSpawn = false -- keep the bar through respawns
local bar = Instance.new("Frame")
bar.AnchorPoint = Vector2.new(0.5, 1)
bar.Position = UDim2.new(0.5, 0, 1, -28)
bar.Size = UDim2.fromOffset(260, 16)
bar.BackgroundColor3 = Color3.fromRGB(24, 22, 32)
bar.Parent = gui
local stroke = Instance.new("UIStroke")
stroke.Color = Color3.fromRGB(255, 255, 255)
stroke.Transparency = 0.7
stroke.Thickness = 1.5
stroke.Parent = bar
local fill = Instance.new("Frame")
fill.Size = UDim2.fromScale(1, 1)
fill.BorderSizePixel = 0
fill.Parent = bar
for _, frame in { bar, fill } do
local corner = Instance.new("UICorner")
corner.CornerRadius = UDim.new(1, 0)
corner.Parent = frame
end
local label = Instance.new("TextLabel")
label.BackgroundTransparency = 1
label.Size = UDim2.fromScale(1, 1)
label.Font = Enum.Font.GothamBold
label.TextSize = 12
label.TextColor3 = Color3.fromRGB(255, 255, 255)
label.TextStrokeTransparency = 0.6
label.ZIndex = 2
label.Parent = bar
gui.Parent = player:WaitForChild("PlayerGui")
local FULL_COLOR = Color3.fromRGB(80, 220, 140)
local LOW_COLOR = Color3.fromRGB(240, 70, 70)
local TIRED_COLOR = Color3.fromRGB(120, 120, 130)
local function getHumanoid(): Humanoid?
local character = player.Character
local humanoid = character and character:FindFirstChildOfClass("Humanoid")
if humanoid and humanoid.Health > 0 then
return humanoid
end
return nil
end
local function drawBar()
local share = stamina / MAX_STAMINA
fill.Size = UDim2.fromScale(share, 1)
-- Green when full, red when empty, grey while tired
fill.BackgroundColor3 = if tired then TIRED_COLOR else LOW_COLOR:Lerp(FULL_COLOR, share)
label.Text = tostring(math.floor(stamina))
end
local function onSprint(_name: string, state: Enum.UserInputState, _input: InputObject): Enum.ContextActionResult
if state == Enum.UserInputState.Begin then
sprintHeld = true
elseif state == Enum.UserInputState.End or state == Enum.UserInputState.Cancel then
sprintHeld = false
end
return Enum.ContextActionResult.Pass
end
ContextActionService:BindAction("StaminaSprint", onSprint, true, Enum.KeyCode.LeftShift, Enum.KeyCode.ButtonL3)
ContextActionService:SetTitle("StaminaSprint", "Sprint")
RunService.Heartbeat:Connect(function(deltaTime: number)
local humanoid = getHumanoid()
if not humanoid then
return
end
local now = os.clock()
local moving = humanoid.MoveDirection.Magnitude > 0
local shouldSprint = sprintHeld and moving and not tired and stamina > 0
-- Change WalkSpeed only when sprinting starts or stops, so speed from other scripts is kept
if shouldSprint and not sprinting then
sprinting = true
normalSpeed = humanoid.WalkSpeed
humanoid.WalkSpeed = normalSpeed + SPRINT_BONUS
elseif not shouldSprint and sprinting then
sprinting = false
humanoid.WalkSpeed = normalSpeed
end
if sprinting then
stamina = math.max(0, stamina - DRAIN_PER_SECOND * deltaTime)
lastUse = now
if stamina == 0 then
tired = true
end
elseif now - lastUse >= REGEN_DELAY then
stamina = math.min(MAX_STAMINA, stamina + REGEN_PER_SECOND * deltaTime)
end
if tired and stamina >= MIN_TO_SPRINT then
tired = false
end
-- Jumping: allowed only with enough stamina (SetStateEnabled works on this device only)
if JUMP_COST > 0 then
local canJump = stamina >= JUMP_COST
if canJump ~= jumpAllowed then
jumpAllowed = canJump
humanoid:SetStateEnabled(Enum.HumanoidStateType.Jumping, canJump)
end
end
player:SetAttribute("Stamina", stamina)
drawBar()
end)
-- Each new character: full bar, jumping allowed, and jumps cost stamina
local function onCharacter(character: Model)
stamina = MAX_STAMINA
sprintHeld = false
sprinting = false
tired = false
jumpAllowed = true
local humanoid = character:WaitForChild("Humanoid") :: Humanoid
humanoid.StateChanged:Connect(function(_old: Enum.HumanoidStateType, new: Enum.HumanoidStateType)
if new == Enum.HumanoidStateType.Jumping and JUMP_COST > 0 then
stamina = math.max(0, stamina - JUMP_COST)
lastUse = os.clock()
end
end)
end
player.CharacterAdded:Connect(onCharacter)
if player.Character then
task.spawn(onCharacter, player.Character)
end
drawBar()
Step 2: What each part does
Drain and refill every frame
RunService.Heartbeat fires once per frame and passes deltaTime, the seconds since the last frame. Multiplying the rates by it makes them "per second" instead of "per frame". A full bar of 100 at 20 per second lasts 5 seconds on a slow laptop and on a fast PC alike.
lastUse is the time stamina was last spent. Stamina only refills once REGEN_DELAY seconds have passed since then, so a player cannot tap Shift on and off to sprint and refill at the same time.
The tired state
When stamina reaches exactly 0, tired becomes true and the bar turns grey. While tired, holding Shift does nothing. Only when the bar refills to MIN_TO_SPRINT does the player get to sprint again. Without this, a player holding Shift on an empty bar would refill a sliver, sprint for one frame, run out, and repeat, which looks like stuttering.
Keeping other speed changes
The script only touches WalkSpeed in the frame a sprint starts or stops. At the start it saves the current speed and adds 10; at the stop it puts the saved value back. A speed pad or a game pass that set WalkSpeed to 24 is kept, and the player sprints at 34. A script that set WalkSpeed to 16 every time sprinting stopped would cancel those boosts.
Jumps that cost stamina
Set JUMP_COST to something like 10 and every jump uses stamina. The script listens for the Humanoid entering the Jumping state and takes the cost away. When the bar has less than one jump left, it calls Humanoid:SetStateEnabled(Enum.HumanoidStateType.Jumping, false) so the character cannot jump at all. Roblox's reference notes that SetStateEnabled does not replicate between server and client, which is fine here: the player's own device is the one that decides whether its character jumps.
The bar
The bar is a ScreenGui made in code, so the script is all you need. ResetOnSpawn = false keeps it on screen through respawns. The fill's width is UDim2.fromScale(share, 1), where share is stamina divided by the maximum. Color3:Lerp mixes two colours, so at half stamina the fill is halfway between red and green.
To place the bar somewhere else, change bar.Position. UDim2.new(0.5, 0, 1, -28) means the middle of the screen across and 28 pixels above the bottom. To match the look of a health bar, put both in the same corner and use the same height.
Step 3: Use stamina in other scripts
The script copies the current stamina into a Stamina attribute on the player every frame. Other LocalScripts can read it without knowing how stamina works. A dash that should only work with enough stamina reads it like this:
-- In any LocalScript: read the stamina the StaminaSprint script keeps
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local function getStamina(): number
local value = player:GetAttribute("Stamina")
return if typeof(value) == "number" then value else 0
end
player:GetAttributeChangedSignal("Stamina"):Connect(function()
if getStamina() < 25 then
-- for example: grey out a dash button
end
end)
Reading works anywhere, but spending stamina should happen in one place, or two scripts can each think the bar has 30 left and both spend it. If a dash should cost stamina, add it to the StaminaSprint script itself, as the sprint guide's Step 3 does. The dash guide has a dash with its own cooldown.
Step 4: Test it
- Open the Output window (Window menu, then Output) and press Play.
- Hold Shift and walk: you speed up and the bar drains from green towards red over 5 seconds.
- Keep holding until it is empty: you slow down, the bar turns grey, and holding Shift does nothing until the number is back to 30.
- Stand still holding Shift: nothing drains.
- Set
JUMP_COSTto 25, play, and jump four times quickly: the fourth jump empties the bar and you cannot jump again until it refills. - To try the phone button, test with Studio's device emulator set to a phone.
Common errors and fixes
If the bar does not show or Shift does nothing, check the Output window first: a red line names the script and the line.
The bar does not appear
Check that StaminaSprint is a LocalScript in StarterPlayerScripts. A Script there does not run, and a LocalScript in Workspace does not run either.
The bar drains but the player does not speed up
Another script is setting WalkSpeed every frame. Search your scripts for WalkSpeed and let only one of them change it.
Sprint speed stays after the bar is empty
Two sprint scripts are bound to Shift. Delete the other one, such as ShiftToSprint or MovementClient from the sprint guide.
The player cannot jump at all
JUMP_COST is higher than MAX_STAMINA, or another script disabled the Jumping state. Set JUMP_COST to 0 to check.
Build it with RoCode
If you would rather not set this up by hand, RoCode can build it in the place you have open. RoCode is an independent AI agent for Roblox Studio, not made by Roblox: you describe the change in a web chat, and its Studio plugin creates the parts and scripts in your Explorer, so there is no code to paste.
Add a stamina bar to my horror game: sprinting on Shift drains it in 4 seconds, it refills after a short rest, and the bar sits under my health bar and turns red when low.
- Can read your place first to find your existing sprint and GUI scripts, then fits the stamina in with them
- Writes the scripts with Create Script and is told to compile-check each one with Check Script
- Sets an undo point before and after each batch of changes, so Ctrl+Z steps back one batch at a time
After a build, RoCode can run its test robot: it plays your game in Studio and reports PASS or FAIL. It checks only what it chose to test, so sprint around yourself too. How RoCode connects to Studio.
Questions
How do you make a stamina bar in Roblox Studio?
Keep a stamina number in a LocalScript, lower it while the player sprints and raise it when they rest, and set the width of a Frame in a ScreenGui to stamina divided by the maximum. Step 1 has the full script.
How do I make stamina regenerate in Roblox?
In RunService.Heartbeat, add REGEN_PER_SECOND * deltaTime to stamina once the player has not sprinted for REGEN_DELAY seconds, and cap it at the maximum with math.min.
How long does a full stamina bar last?
MAX_STAMINA divided by DRAIN_PER_SECOND. With 100 and 20 it is 5 seconds. Change either number to make it longer or shorter.
Can I make jumping use stamina?
Yes. Set JUMP_COST above 0. The script takes the cost on every jump and disables jumping when there is not enough left.
Does stamina save when the player leaves?
No, and it usually should not. It refills in a few seconds. A maximum stamina that players upgrade is different: save that number on the server with a DataStore.
Sources and further reading
- Roblox Creator Docs: Humanoid. WalkSpeed, MoveDirection, StateChanged and SetStateEnabled
- Roblox Creator Docs: RunService. Heartbeat and its deltaTime
- Roblox Creator Docs: ContextActionService. BindAction and touch buttons
- Roblox Creator Docs: ScreenGui. ResetOnSpawn
How the code was checked: every script on this page passes Luau's strict type checker against Roblox's API definitions (luau-lsp, 14 November 2026). A type check catches misspelt APIs and wrong types, not game logic, so playtest in Studio before you publish.