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.
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)
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