How to go about making a stamina system?

Im trying to create a basic stamina system for movement in my game. My idea is that doing things like running/dashing will take away stamina. It sounds simple in thought but how would i go about making this? anotehr question is how would i scale GUI based off of the stamina value?

Have a stamina intvalue/numbervalue somewhere in the player that the server can use and when the player Runs you could use a while loop to remove stamina points incrementally until 0. Like

local staminapoints = player.Stamina

while runningtoggle do -- running toggle can be any bool determining if a player is running
   staminapoints -= 1
   task.wait(0.1)
end

and another while loop to regenerate said stamina if the player is no longer running.

If you want a gui to scale(assuming you stamina bar goes from left to right and you stamina is 100) you can use a simple formula in a .Changed function

local StaminaBar = --StaminaBarLocation

staminapoints.Changed:Connect(function()
   local GoalSizeX = staminapoints.Value/100
   StaminaBar:TweenSize(Udim2.new(GoalSizeX,0,StaminaBar.Size.Y.Scale,0))
end)
1 Like
local Stamina = 10
local CurrentStamina = Stamina
local Frame = script.Parent

-- when the player runs, subtract the value of the variable every certain interval
if CurrentStamina > 0 then
	CurrentStamina -= 1
end

-- when player stops running, regenerate stamina every certain interval
if CurrentStamina < Stamina then
	CurrentStamina += 1
end

-- and to scale the UI, all you have to do is divide the current stamina by the default value
Frame.Size = UDim2.fromScale(CurrentStamina/Stamina,0)
1 Like

should the script be completely client sided? it involves player movement so iā€™d assume the server should be involved

The server would be able to handle changing walkspeed or body movers if it was a dash move and the client could just fire a remote event with a serverside toggle

1 Like

I see. This is a sprint to run type system so it could kind of work. My idea is that the client will detect inputs, fire the server, and the server will adjust the walkspeed and the stamina amount. My main issue is with the input ended. if the input ends i can maybe fire the same event with a different argument and adjust the walkspeed and stamina ammount accordingly.

Im just writing down my thoughs on how to go about this, i am sorry :sweat_smile:

You can have a boolvalue created on the player when they join and whenever they fire a remote event without arguments for input start and end the server can use that bool as a toggle

1 Like