Stamina regen system

I want to make my stamina regen to be similar to the stamina in souls game, where when stamina is lowered, it will wait a bit and start regenerate quickly.

This is the script I stole from roblox health regen system:

local plr = game:GetService("Players").LocalPlayer
local StatsFolder = script.Parent

local Stamina = StatsFolder:WaitForChild("Stamina")
local MaxStamina = StatsFolder:WaitForChild("MaxStamina")
local Mana = StatsFolder:WaitForChild("Mana")
local MaxMana = StatsFolder:WaitForChild("MaxMana")

local REGEN_RATE = 10/100
local REGEN_STEP = 0.1

while true do
	while Stamina.Value < MaxStamina.Value do
		local dt = wait(REGEN_STEP)
		local dh = dt * REGEN_RATE * MaxStamina.Value
		Stamina.Value = math.min(Stamina.Value + dh, MaxStamina.Value)
	end
	Stamina:GetPropertyChangedSignal("Value"):Wait()
end

1 Like
while Stamina.Value < MaxStamina.Value do
	Stamina.Value += 1
	task.wait()
emd

this is just a a example now you need to implement it
add this after the part you want to start your regen

You should not control the stamina within the client, I very strongly recommend you move your stamina system to the server

As zander said, I would recommend putting this on the server side but here is a script you could use to implement your desired mana regen system.

local remotes = game:GetService("ReplicatedStorage").Remotes

local timer = 0
local goal = 4
local REGEN_STEP  = 5
local MAX_STAMINA = 100
local regening = false
local interrupted = false
local counting = false

local function checkStam(player, stamCost)
	
	local stam = player.Character:WaitForChild("stamina")
	local stamVal = stam.Value
		
	if stam.Value - stamCost < 0 then
		return false
	else
		timer = 0
		if regening == true then
			interrupted = true
		end
		
		stam.Value -= stamCost


		spawn(function()

			if counting == false then
				counting = true
				repeat 
					wait(1)
					timer += 1
		
					print(timer)
				until timer == goal
				print("GotPastTheFirstOne")
				regening = true
				while stam.Value < MAX_STAMINA do
					if interrupted then
						wait(goal)
						interrupted = false
					end
					wait(0.1)
					if stam.Value + REGEN_STEP > MAX_STAMINA or stam.Value == MAX_STAMINA then
						stam.Value = MAX_STAMINA
						break
					else
						stam.Value += REGEN_STEP
					end
				end
				regening = false -- define these two bools outside of the function
				counting = false
		
			end
		end)
		
		
		return true
	end
	
end




remotes.StamiaRe.OnServerInvoke = checkStam

In order to make this script work, you will need to create a remote function and bind it to checkStam (or whatever function name you want). Use InvokeServer on the client script to make it work. If you need help understanding remote functions you can use this resource Remote Functions and Events. I would most likely recommend storing player data where only the server can access so some tweaking may be required for you to get this script to work.

1 Like

Adding onto this, I recommend making Stamina an attribute of the player and changing it on the server side.