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)
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)