Stamina Script Not Working

I am trying to add a stamina script into my game, but everything I try seems to not work.
image

First of all you have a random end,
Second of all you provided us with no information at all, how would you like this stamina to work

Sorry. So my game is a football training game, and I want it to when a player clicks shift they use some of their stamina. I want a stamina bar to go down as they use it. Once all of it is used there is a cooldown.

Do you already have it so you detect shift key press, if not a simple

local UIS = game:GetService("UserInputService")

local shiftHold = false
local lastKey = nil

UIS.InputBegan:Connect(function(key)
    if key.KeyCode == Enum.KeyCode.LeftShift then
        shiftHold = true
    end
    lastKey = key.KeyCode
end)

UIS.InputEnded:Connect(function()
    if lastKey == Enum.KeyCode.LeftShift then
        shiftHold = false
    end
    lastKey = nil
end

This will give you shift hold detection.

Where would I place this script?

I suggest you try learning how to script first before attempting to make something like a stamina script.

1 Like

1 script in StarterPlayerScripts (local script)

1 Like

image

No, you put

it lastKey == Enum.KeyCode.LeftShift then

It’s supposed to be if

image

Create a LocalScript in StarterPlayer.StarterPlayerScripts and try this:

local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")
local LocalPlayer = Players.LocalPlayer

local Shift = false
local Stamina = 20 -- Change if you want
local Cooldown = 3 -- Change if you want
local Seconds = 1 -- Change if you want

UserInputService.InputBegan:Connect(function(Input)
    if Input.KeyCode == Enum.KeyCode.LeftShift then
        LocalPlayer.Character.Humanoid.WalkSpeed = 32 -- Change if you want
        Shift = true
        repeat
            wait(Seconds / 2) -- 0.5
            Stamina = Stamina - 1
        until Stamina == 0 or Shift == false
    end
end)

UserInputService.InputEnded:Connect(function(Input)
    if Input.KeyCode == Enum.KeyCode.LeftShift then
        LocalPlayer.Character.Humanoid.WalkSpeed = 16
        Shift = false
        wait(Cooldown)
        repeat
            wait(Seconds) -- 1
            Stamina = Stamina + 1
        until Stamina == 20 or Shift == true
    end
end)
1 Like

Change the it to if, I’m on mobile and didn’t realize autocorrect did it’s stupid if to it correction

Now if I click shirt I am running faster?

Ok, cool you saved me a hassle of mobile typing, I was just trying to walk him through the steps

If you hold shift you will run faster and your stamina will go down every 0.5 seconds until you stop holding Shift or until your stamina is 0 and when u stop holding shift you will stop running and your stamina will go up every 1 seconds until you start running again or until your stamina is max

1 Like

Is there an easy way I could add a stamina gui?

So do u already have your GUIs?

Not currently. But I can easily add one.

It’s really easy to make a stamina GUI with this system i made. First you will need to parent the script to your ScreenGui and change the script to:

local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")
local LocalPlayer = Players.LocalPlayer

local ScreenGui = script.Parent
local BarBackground = ScreenGui.Background
local Bar = BarBackground.Bar

local Shift = false
local Stamina = 20 -- Change if you want
local Cooldown = 3 -- Change if you want
local Seconds = 1 -- Change if you want

UserInputService.InputBegan:Connect(function(Input)
    if Input.KeyCode == Enum.KeyCode.LeftShift then
        LocalPlayer.Character.Humanoid.WalkSpeed = 32 -- Change if you want
        Shift = true
        repeat
            wait(Seconds / 2) -- 0.5
            Stamina = Stamina - 1
        until Stamina == 0 or Shift == false
    end
end)

UserInputService.InputEnded:Connect(function(Input)
    if Input.KeyCode == Enum.KeyCode.LeftShift then
        LocalPlayer.Character.Humanoid.WalkSpeed = 16
        Shift = false
        wait(Cooldown)
        repeat
            wait(Seconds) -- 1
            Stamina = Stamina + 1
        until Stamina == 20 or Shift == true
    end
end)

while wait() do
    Bar:TweenSize(UDim2.new(Stamina / 20, 0, 1, 0), "Out", "Quad")
end