How can I make a stamina bar that works with my sprint script?

So I have a sprint button right now and I have a script that makes you sprint, but I want to make a stamina bar GUI that goes down and lowers your sprinting speed (when you have no stamina make it your default walkspeed) and goes back up when you stop sprinting. I have a sprint button instead of shift to sprint and I want to keep it that way if possible. Does anyone know how I can do this? (I’ll attach the existing code which is in a TextButton that makes you sprint below)

Ok that seemed confusing so basically what I want to do is when you click the sprint button your stamina goes down and then when you click the button again to walk I want it to go back up again

local button = script.Parent
local Players = game:GetService('Players')
local sprinting = false

local function sprint()
	local player = Players.LocalPlayer
	game.Workspace.Click:Play()
	if not sprinting then
		sprinting = true
		button.Text = 'Sprint: On'
		player.Character.Humanoid.WalkSpeed = 30
		script.Parent.Parent.SprintNotif:TweenPosition(UDim2.new(0.012, 0, 0.861, 0),"In","Sine",0.1)
		script.Parent.Parent.SprintNotif.TextLabel.Text = ("Sprinting")
	else
		sprinting = false
		button.Text = 'Sprint: Off' 
		player.Character.Humanoid.WalkSpeed = 16
		script.Parent.Parent.SprintNotif:TweenPosition(UDim2.new(0.012, 0, 0.861, 0),"In","Sine",0.1)
		script.Parent.Parent.SprintNotif.TextLabel.Text = ("Walking")
		wait(1)
		script.Parent.Parent.SprintNotif:TweenPosition(UDim2.new(0.012, 0, 1, 0),"Out","Sine",0.1)
	end
end

button.MouseButton1Click:Connect(sprint)
4 Likes

Well for starters what you can do is keep note of their max stamina and decrease it in a loop while they keep sprinting, once the value reaches 0 then make their speed normal and refill the bar. Let me know if you need more details : )

Could I make the stamina value in a text label so that the bar like tweens with the label so when you press sprint it goes down and when you click again it goes up again idk how it would work but its an idea that might work

Here is an example video of how I used it in an old game

Layout for the UI (Used grid layout most likely to keep the bar in place which made it easier to size)
image

local Running = false

local StamBar = player.PlayerGui.CoreUI.CoreFrame.StaminaBar.UIGridLayout
local StamText = StamBar.Parent.Parent.Parent.StaminaText
local StaminaMax = 100
local Stamina = 100


-- Running --
local function Run(input)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		Running = true
		humanoid.WalkSpeed = 30
		
		while Running and Stamina > 0 do
			Stamina -= 1		
			StamText.Text = "Stamina " .. Stamina 
			StamBar.CellSize = UDim2.new(Stamina/StaminaMax, 0, 0.6, 0)			
			wait(.1)
		end
		
		if Stamina <= 20 then
			humanoid.WalkSpeed = 16
		end
		
	end
end
UIS.InputBegan:Connect(Run)



-- Walking --
local function Walk(input)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		Running = false
		humanoid.WalkSpeed = 16
		
		while not Running and Stamina < 100 do
			Stamina += 1		
			StamText.Text = "Stamina " .. Stamina 
			StamBar.CellSize = UDim2.new(Stamina/StaminaMax, 0, 0.6, 0)			
			wait(.2)
		end
		
	end
end
UIS.InputEnded:Connect(Walk)



Though this code is not nearly good at all and I dont recommend you use some parts because its bad practice, take a note of the concept of how it was put together and this should help you make yours. Again, if you have any more questions let me know!

6 Likes

LATE EDIT: YOU SHOULD NOT DO THE REGENERATION PART OR THE DETECTION PART IN LOCAL SCRIPTS, DO IT IN SERVER SCRIPTS OTHERWISE EXPLOITERS MAY BE ABLE TO MODIFY THE AMOUNT OF STAMINA AND CAN EASILY GIVE THEMSELVES INFINITE STAMINA. SAME THING GOES FOR SPEED (MAKE SURE TO HAVE AN ANTICHEAT FOR IT AS WELL).

Updating GUI

Okay so before we get into this, we have to learn how to save GUI’s, the easiest and probably the best way doing this is using UDmin2 You can simply create a Function in order to update the gui.

Okay so we figured out what UDim2 and functions are, but now how do we actually cause the stamina to update? Well you can create a function as mentioned above, then by using this code:

stamina = math.clamp(stamina, 0, 100)
yourGuiBar.Size = UDim2.new((1/100) * stamina, 0, 1, 0)

So in here, we used math.clamp in order to detect between the minimum value and the maximum, otherwise it would just go out of the bar. Then, we can use UDim2.new to resize the gui resulting it to update.

Saving Stamina

To save stamina values, one of which is the easiest way: Variables or NumberValue.
Make sure you create a local variable and set the stamina to 100 otherwise they will start with 0 stamina. It’s simple as that.

Reducing Stamina

You can reduce stamina by using a while loop or repeat until to reduce the stamina.

But wait, how can we detect if the user is moving?

In this case, you can use humanoid.MoveDirection.Magnitude to detect if the user is moving. Then, you can assign a variable and call it, lets say, running, or by making a BoolValue, then you can set either of those to true and then you can use an if statement and then you can reduce their stamina whilst looping.

Regenerating Stamina

Okay, so the stamina does get reduced, but then, how do we regenerate the stamina? Well it’s pretty simple, you can again, use a loop and check if the user is running or not. If they are not running then regenerate the users stamina.

That’s All!

That’s all there is to it! If you didn’t understand something or if you have any questions/doubt then be sure to ask.

10 Likes

local button = script.Parent
local Players = game:GetService(‘Players’)
local sprinting = false

local function sprint()
local player = Players.LocalPlayer
game.Workspace.Click:Play()
if not sprinting then
sprinting = true
button.Text = ‘Sprint: On’
player.Character.Humanoid.WalkSpeed = 30
script.Parent.Parent.SprintNotif:TweenPosition(UDim2.new(0.012, 0, 0.861, 0),“In”,“Sine”,0.1)
script.Parent.Parent.SprintNotif.TextLabel.Text = (“Sprinting”)
else
sprinting = false
button.Text = ‘Sprint: Off’
player.Character.Humanoid.WalkSpeed = 16
script.Parent.Parent.SprintNotif:TweenPosition(UDim2.new(0.012, 0, 0.861, 0),“In”,“Sine”,0.1)
script.Parent.Parent.SprintNotif.TextLabel.Text = (“Walking”)
wait(1)
script.Parent.Parent.SprintNotif:TweenPosition(UDim2.new(0.012, 0, 1, 0),“Out”,“Sine”,0.1)
end
end

button.MouseButton1Click:Connect(sprint)

You will have to make a function that downgrades the sprinting speed, look at the front page of the wiki to see how to start and then a function that upgrades the sprinting speed. When you make those functions, put them inside a while true do wait(1) end loop and then make a variable that you can change in that loop that will make the script break out of the loop.

1 Like