Increasing a Player's stamina every second

You could iterate through the players and increase their stamina if it can be increased

local function IncreaseStamina(player, amount)
    -- Code to increase stamina (should clamp the value to max value)
end

coroutine.wrap(function()
    local nextUpdate = tick()
    local staminaIncrementConnection = game:GetService('RunService').Heartbeat:Connect(function()
        if tick() - nextUpdate > 1 then
            nextUpdate += 1
            for _, player in pairs(game:GetService('Players'):GetPlayers()) do
                IncreaseStamina(player, 10)
            end
        end
    end)
end)()

The problem is that it is synced when approached in that method. The method I described is asynchronous and does not add stamina simultaneously instantaneous.

1 Like

Yup, further more your method is also better optimized.