Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How do i build a stamina bar?

Asked by 7 years ago

I'm trying to build a stamina bar GUI for my game. I already built a Health Gui from AlvinBlox's youtube channel. Don't worry i tried to understand about the guis. I started thinking how to start scripting. I tried setting an IntValue to stamina. Such as 100. And then i told the script: if the player's walkspeed is above 16 then substract 1 from the so-called 'STAMINA POINTS' each second. Here's what my code sort of looked like. Am I going the right way? I used a local script. What else could I use?


local player = game.Players.LocalPlayer local humanoid = player.Character.Humanoid --------------------- HUMANOID? local staminabar = script.Parent -------------------------THE ACTUAL STAMINA BAR local staminaframe = script.Parent.Parent ---------------------FRAME THAT HOLD THE BAR local staminamax = 100 ----------------------------MAXIMUM STAMINA humanoid.Changed:connect(function() local stamina = Instance.new('IntValue', player) stamina.Name = 'StaminaVal' stamina.Value = 100 while true do if humanoid.WalkSpeed > 16 then ------------if walkspeed > 16 then waste stamina stamina.Value = stamina.Value - 1 wait(1) end end script.Parent.Size = UDim2.new(stamina.Value/staminamax,0,1,0) end)
0
Well, it'll only work if the player's WalkSpeed value is greater than 16, and not when running/ walking/ etc.; if that's your intention, then I recommend using the Magnitude value of Velocity from the player's torso, which is a bit complicated & may be overdoing things. :P TheeDeathCaster 2368 — 7y
0
I forgot to mention this, but i really only want to waste stamina when WalkSpeed is faster than 16. So long as its above 16 stamina decreases. Which is really boring. If you have any other suggestions on how i should use the stamina bar please let me know. Mariojumper5 26 — 7y
0
i ALSO FORGOT TO MENTION THAT IT DOESN`T WORK Mariojumper5 26 — 7y
0
whats the line after end for? AnotherPerson_999 28 — 6y

1 answer

Log in to vote
0
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
7 years ago
Edited 7 years ago

You're almost there. Just need to make a couple of changes:

  • 1) Create the Stamina value on the server. Reference it on the client. This can be done using a PlayerAdded event with a Script in ServerScriptStorage.

  • 2) Add the walkspeed condition to your while loop, so it doesn't run forever.

  • 3) Add a debounce to prevent multiple firings of the event subtracting your value.

local player = game.Players.LocalPlayer

repeat wait() until player.Character --Wait for the character

local humanoid = player.Character.Humanoid
local staminaframe = script.Parent.Parent
local staminabar = script.Parent
local staminamax = 100
local db = false --Step 3

local stamina = player:WaitForChild("StaminaVal") --Step 1

humanoid.Changed:connect(function()
    if not db then
        db = true
        while humanoid.WalkSpeed > 16 and wait(1) do --Step 2
            stamina.Value = stamina.Value - 1
            staminabar.Size = UDim2.new(stamina.Value/staminamax,0,1,0)
        end
        db = false
    end
end)
Ad

Answer this question