I’ve been working on the stamina system for my Backrooms game and for some reason all of a sudden the stamina won’t regen. Here’s my script:
local stamina = script.Stamina
local maxStamina = script.maxStamina
local staminaDrain = 3.3 -- Stamina drained per second of running
local staminaRegen = 10 -- Stamina regened per second of not running
local running = false
maxStamina.Value = 100
stamina.Value = maxStamina.Value
while stamina.Value < maxStamina.Value and running == false do
print("Not running")
task.wait(1 / staminaRegen)
stamina.Value += 1 / staminaRegen
print(stamina.Value)
if stamina.Value > maxStamina.Value then
stamina.Value = maxStamina.Value
end
end
When I playtest and set my stamina to 50, it doesn’t print “Not running” so I think the issue is in the while loop.
I edited your script a bit and hopefully this works:
local stamina = script.Stamina
local maxStamina = script.maxStamina
local staminaDrain = 3.3 -- Stamina drained per second of running
local staminaRegen = 10 -- Stamina regened per second of not running
local running = false
maxStamina.Value = 100
stamina.Value = maxStamina.Value
repeat
print("Not running")
stamina.Value = math.min(task.wait() * staminaRegen + stamina.Value, maxStamina.Value)
print(stamina.Value)
until running or (stamina.Value == maxStamina.Value)
If it doesn’t then there’s most likely something else in the rest of your script that’s causing it to break
Then as I explained most likely there’s something in the rest of your script that’s causing it to break. Please show it to us as it would be quite helpful, if you can of course
The reason why I’m saying this is because both the script you wrote and the script I wrote have nothing in them that would cause the problems you’re experiencing
What are you doing to set your stamina to 50 when playtesting?
Sorry to bother you again, but I kept testing and I made one you might like:
local stamina = script:WaitForChild("Stamina")
local maxStamina = script:WaitForChild("maxStamina")
local staminaDrain = 3.3 -- Stamina drained per second of running
local staminaRegen = 10 -- Stamina regened per second of not running
local running = false
maxStamina.Value = 100
stamina.Value = maxStamina.Value
stamina.Changed:Connect(function(value)
if running then return end
if value < maxStamina.Value then
print("Not running")
stamina.Value = math.min(task.wait() * staminaRegen + value, maxStamina.Value)
print(stamina.Value)
end
end)
The way your script and the script I first suggested work have a drawback where they only work once, this script solves that problem without the need of using a second loop