Stamina failing to regen

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.

2 Likes

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

1 Like

Nope, still doesn’t work at all.

1 Like

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

1 Like

You’ve already seen the entire script. It’s the script I had in my post, with the while loop replaced by the repeat loop you gave me.

1 Like

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?

1 Like

I run the command game.Players.samurai60001.PlayerScripts.Stamina.Stamina.Value = 50
The command works because I see the stamina bar lower to 50%

1 Like

Also, is the script a server Script or a LocalScript?

1 Like

LocalScript, but never mind I fixed it. I just had to change the stamina.Value < maxStamina.Value and running == false to not running

1 Like

If that was what was causing your script to break then I don’t understand how my script failed to work since it doesn’t have that problem

1 Like

Neither do I. The one issue is that my current script is pretty unoptimised. But if my 2 core CPU can run it I’d say it’s fine.

2 Likes

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

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.