Help on stamina bar script

There are a couple of Issues with your Script, some are minor, while some are bigger and completely unnecessary to have at all.

To Start off, I see no purpose to Have a sprintingPlayer table, you can just use this RemoteEvent to fire when the State Changes, Plus, the Player is the first argument within a Server Event, This will make it much easier for you to keep track of the Player.

For your IntValue creation, There is a reason Developers never use the Second Argument to Parent:

It causes Preformace issues.
You should Also first (before anything else) Apply the Name, then the Properties, this should be the “proper” way to Create an Instance

local stamina = Instance.new("IntValue") -- Create Instance
stamina.Name = "Stamina" -- Prioritize this FIRST (Always)
stamina.Value = maxStamina -- Apply Properties
stamina.Parent = player -- Prioritize this LAST (Always)

Your Server script doesn’t require a while loop to function, if anything its a waste of Server Resources to be using it for this purpose, this is what makes an inefficient system, Instead it would be best Practice to use math.clamp for this case, math.clamp will limit the Value between the minimum value and maximum value, this is useful because because if a number is over or below a limit, it will automatically be set to this value, Here is an Example:

isStamina.Value = math.clamp(isStamina.Value - 1, 0, 100)
-- 0 is the minimum
-- 100 is the maximum

or, if you only plan to set one limit for a certain part, you can use math.min() or math.max() to handle this:

isStamina.Value = math.max(0, isStamina.Value - 1) -- returns the Largest Value
isStamina.Value = math.min(100, isStamina.Value - 1) -- returns the Smallest Value

Why?

4 Likes