[SOLVED] Stamina bar script

Ok so I’m trying to make a stamina bar script and I’ve tried many things. I want the script to have a small bobbing animation when it goes down and when it gets to 0, it deletes the stamina bar. My problems are 1. The stamina goes down faster than 1 second 2. The stamina bar grows bigger instead of shrinking 3. The stamina bar doesn’t dissappear after the value is less than 0.
How can I fix this?
Script:

local Players = game:GetService("Players")
local Player = game.Players.LocalPlayer
local Character = Player.Character

local UIS = game:GetService("UserInputService")
local IsRunning = false
local MAX_STAMINA = 20
local currentStamina = 20

UIS.InputBegan:Connect(function(input)
    if input.UserInputType == Enum.UserInputType.Keyboard then
        if input.KeyCode == Enum.KeyCode.LeftShift then
            if currentStamina >= 1 then
                IsRunning = true
                Character.Humanoid.WalkSpeed = 18

                while IsRunning == true do
                    if currentStamina <= 0 then
                        IsRunning = false
                        Character.Humanoid.WalkSpeed = 12
                        script.Parent.Visible = false
                    elseif currentStamina >= 0 then
                        currentStamina = math.max(currentStamina - 0.05, 0)
                        script.Parent:TweenSize(UDim2.fromScale(currentStamina / MAX_STAMINA, 1), Enum.EasingDirection.Out, Enum.EasingStyle.Back, 1, true)
                    end
                    task.wait(1)
                end
            end
        end
    end
end)

UIS.InputEnded:Connect(function(input)
    if input.UserInputType == Enum.UserInputType.Keyboard then
        if input.KeyCode == Enum.KeyCode.LeftShift then
            if currentStamina <= 0 then
                IsRunning = false
                Character.Humanoid.WalkSpeed = 12
                script.Parent.Visible = false
            end
            IsRunning = false
            Character.Humanoid.WalkSpeed = 12

        end
    end
end)

Hello, I have rewrote your script and this one seems to work fine. I added a coroutine function to fix the first issue stated. I don’t really know why you’d be experiencing the second and third issue so I just rewrote the script as how I would do it. Btw the “script.Parent” is referring to the actual stamina bar not the holder if you have it in one.

local UserInputService = game:GetService("UserInputService")

local Players = game:GetService("Players")
local player = Players.LocalPlayer

local char = player.Character
local hum = char:WaitForChild("Humanoid")

local isRunning

local maxStamina = 20
local curStamina = 20

local running
function began(input)
	input = input.KeyCode

	if input == Enum.KeyCode.LeftShift then
		if curStamina >= 1 then
			isRunning = true
			hum.WalkSpeed = 18

			running = coroutine.create(function()
				while isRunning do task.wait(1)
					if curStamina <= 0 then
						isRunning = nil
						hum.WalkSpeed = 12
						
						script.Parent.Visible = false
						coroutine.yield(running)
					else
						curStamina = math.max(curStamina - .5, 0)
						script.Parent:TweenSize(UDim2.fromScale((curStamina / maxStamina), 1), "Out", "Back", 1, true)
					end
				end
			end)

			coroutine.resume(running)
		end
	end
end

function ended(input, cancel)
	if cancel then return end
	input = input.KeyCode

	if input == Enum.KeyCode.LeftShift then
		if curStamina <= 0 then
			script.Parent.Visible = false
		end

		isRunning = nil
		hum.WalkSpeed = 12
		
		coroutine.close(running)
	end
end

UserInputService.InputBegan:Connect(began)
UserInputService.InputEnded:Connect(ended)

while true do
currentStamina = math.min(currentStamina + 0.1, MAX_STAMINA)
script.Parent:TweenSize(UDim2.fromScale(currentStamina / MAX_STAMINA, 1), Enum.EasingDirection.Out, Enum.EasingStyle.Quad, 1, true)
wait(1)
end

You could use tweens to fade the bar out or in, instead of waiting 1 second.
To make it wait 1 second, you could use a while loop and wait a second in each iteration.

Sorry, I forgot to say I already fixed the issue. If you can, please help me here instead.

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