Help With Stamina System Script

Hello! I have recently spent a while creating a movement system for my game. One essential aspect is a stamina system. After managing to create the whole running and draining stamina system with my very limited scripting skills, I was happy that everything worked. However, the last thing I needed to add was a way to regain stamina when your weren’t running. I thought this would be easy but it literally will not work. I can get the stamina to go up when you are walking or not moving, but it just keeps going up. I have set max stamina at 500 and it just goes beyond that every time. I do not know what to do anymore and cannot really find any videos on what I need. Can someone please help/guide me at least.

Here is the place where I am trying to script the regaining of stamina. It is inside of a local script which is inside the stamina bar gui. Fyi, stamina is an “int value” inside of the player.

Instead of using ==, use => on line 22, this will check if stamina is more than 500.

Adding to what @PiercedMissile said, you should use math.clamp to make sure the Stamina value stays between two values of your choice.

You could do this in the .Changed event perhaps.

Stamina.Changed:Connect(function()
   Stamina.Value = math.clamp(Stamina.Value, 0, MaxStamina)
end)

The first parameter is the current value, the second parameter is the minimum value the first parameter can have and the third parameter is the maximum value the first parameter can have.