Problem with stamina system

i have a problem with my stamina system, the system is used everytime the player mines a rock, the stamina decreases and waits a few seconds before it regens again. I have a problem where everytime i mine a rock, the stamina bar just stops there, doesn’t do anything and doesn’t regen
robloxapp-20231106-2038214.wmv (614.8 KB)
Here is the script :

local rs = game:GetService("ReplicatedStorage")
local canmine = rs.Tool.Pickaxe.CanMine or rs.Tool.UpgradedPickaxe.CanMine
local mining = rs.Mining

local UIS = game:GetService("UserInputService")

local currentStamina = 250
local maxStamina = 250
local isRunning = false

currentStamina = math.clamp(currentStamina, 0, maxStamina)

local function updateStamina()
	if currentStamina > 0 then
		currentStamina = currentStamina - 25
		script.Parent:TweenSize(UDim2.new(0, 21, currentStamina / maxStamina, 0), "Out", "Linear", 0)
	end

	while currentStamina < 250 do
		currentStamina = currentStamina + 0.5
		script.Parent:TweenSize(UDim2.new(0, 21, currentStamina / maxStamina, 0), "Out", "Linear", 0)
	end
end

mining.OnServerEvent:Connect(function()
	updateStamina()
end)

if you want a auto updating stamina system, try to use while loop. Because your script only checks once you try to mine, so when you stopped mining the UpdateStamina() for regen won’t work since any action done with the Mining is stopped.

1 Like

What i do most of the time is just add “Value.Changed:Connect(function() end)”
or While true do loop thingy :d

local rs = game:GetService("ReplicatedStorage")
local canmine = rs.Tool.Pickaxe.CanMine or rs.Tool.UpgradedPickaxe.CanMine
local mining = rs.Mining

local UIS = game:GetService("UserInputService")

local currentStamina = 250
local maxStamina = 250
local isRunning = false

currentStamina = math.clamp(currentStamina, 0, maxStamina)

local function updateStamina()
	if currentStamina > 0 then
		currentStamina = currentStamina - 25
		script.Parent:TweenSize(UDim2.new(0, 21, currentStamina / maxStamina, 0), "Out", "Linear", 0)
	elseif currentStamina < 250 do
		currentStamina = currentStamina + 0.5
		script.Parent:TweenSize(UDim2.new(0, 21, currentStamina / maxStamina, 0), "Out", "Linear", 0)
	end
end

currentStamina.Changed:Connect(function()
	mining.OnServerEvent:Connect(function()
		updateStamina()
	end)
end)

I think you’ve just underestimated how fast this code will run, the tweens will simply cancel each other out.
Try this with a wait in the middle, and I updated the tween durations slightly.

local rs = game:GetService("ReplicatedStorage")
local canmine = rs.Tool.Pickaxe.CanMine or rs.Tool.UpgradedPickaxe.CanMine
local mining = rs.Mining

local UIS = game:GetService("UserInputService")

local currentStamina = 250
local maxStamina = 250
local isRunning = false

currentStamina = math.clamp(currentStamina, 0, maxStamina)

local function updateStamina()
	if currentStamina > 0 then
		currentStamina = currentStamina - 25
		script.Parent:TweenSize(UDim2.new(0, 21, currentStamina / maxStamina, 0), "Out", "Linear", 0.5)
	end
        task.wait(0.5)
	while currentStamina < 250 do
          task.wait(0.1)
		currentStamina = currentStamina + 0.5
		script.Parent:TweenSize(UDim2.new(0, 21, currentStamina / maxStamina, 0), "Out", "Linear", 0.1)
	end
end

mining.OnServerEvent:Connect(function()
	updateStamina()
end)
1 Like

while @OufGuy 's way can work, for a simplified version, you’d just separate the Detection for Regenerating.

while true do
  if currentStamina <= 250 then
    currentStamina = currentStamina + 0.5
    script.Parent:TweenSize(UDim2.new(0, 21, currentStamina / maxStamina, 0), "Out", "Linear", 0)
  elseif currentStamina == 250 then
    print("User's Stamina is Full") -- you can replace this.
  end
end
1 Like

There is an error saying “Players.aryaanggahiro.PlayerGui.Stamina.Bar.Stamina.Script:30: Script timeout: exhausted allowed execution time”

it doesn’t change anything for me, it still doesn’t work

there is an error with your script : "Players.aryaanggahiro.PlayerGui.Stamina.Bar.Stamina.Script:26: attempt to index number with ‘Changed’ "

Add a task.wait() in the while loop, it’s running too fast

true, probably becuase its not a value but inside script i think

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