Stamina bar not working

I want to make a stamina bar that changes according to a value stored in a folder inside the player’s character. When I change the stamina, the bar just disappears.


Here’s my script in a frame in startergui.

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local Stamina = character:WaitForChild("CharacterValues"):WaitForChild("Stamina")
local StaminaBeforeChange = Stamina.Value

Stamina:GetPropertyChangedSignal("Value"):Connect(function()
	if StaminaBeforeChange > Stamina.Value then
		script.Parent.Size = UDim2.new(StaminaBeforeChange - Stamina.Value / 100, 1, 0, 0)
		StaminaBeforeChange = Stamina.Value
	else
		script.Parent.Size = UDim2.new(Stamina.Value - StaminaBeforeChange / 100, 1, 0, 0)
		StaminaBeforeChange = Stamina.Value
	end	
end)

3 Likes

hi
the problem is how you calculate the size

code:

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local Stamina = character:WaitForChild("CharacterValues"):WaitForChild("Stamina")
local StaminaBeforeChange = Stamina.Value

Stamina:GetPropertyChangedSignal("Value"):Connect(function()

    local stamina = math.clamp(Stamina.Value, 0, 100)
    script.Parent.Size = UDim2.new(stamina / 100, 0, 1, 0)

    StaminaBeforeChange = Stamina.Value
end)
1 Like

If Stamina.Value is between 0-100, simply:

Stamina:GetPropertyChangedSignal("Value"):Connect(function()
	script.Parent.Size = UDim2.new(Stamina.Value / 100, 1, 0, 0)
end)

would work

2 Likes

This doesn’t work, only makes it larger on both axis

2 Likes

The problem still happens with this change.

1 Like

Larger? That’s not possible unless whatever thing you wrote for the stamina adds to it instead of substracting

1 Like

Not sure what you mean by resting, but this is my code for the stamina:

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local CV = Character.CharacterValues
local Humanoid = Character:FindFirstChildOfClass("Humanoid")

local RunningSpeed = 32
local CurrentStamina = CV.Stamina
local Sprinting = CV.Sprinting

Humanoid:GetPropertyChangedSignal("WalkSpeed"):Connect(function()
	while true do
		task.wait(1)
		if Sprinting.Value == true then
			CurrentStamina.Value -= 1
		elseif not (Sprinting.Value == true) and CurrentStamina.Value < 100 then
			CurrentStamina.Value += 1
		end

	end
end)	
1 Like

Also, you might have to change the AnchorPoint of the bar to 0 on the X axis so that it can remain to the left when resizing it

1 Like

The anchor point is already 0 for both X and Y.

1 Like

try
UDim2.new(stamina / 100, 0, script.Parent.Size.Y.Scale, script.Parent.Size.Y.Offset)
maybe this will work

1 Like

Oh, yes, I didn’t catch that he’s using 0 for the Y size

2 Likes

Now the size isn’t changing. Overall updated code:

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local Stamina = character:WaitForChild("CharacterValues"):WaitForChild("Stamina")
local StaminaBeforeChange = Stamina.Value

Stamina:GetPropertyChangedSignal("Value"):Connect(function()

    local stamina = math.clamp(Stamina.Value, 0, 100)
	script.Parent.Size = UDim2.new(stamina / 100, 0, script.Parent.Size.Y.Scale, script.Parent.Size.Y.Offset)

    StaminaBeforeChange = Stamina.Value
end)
1 Like

First, we have to get 1% of the size of the frame, so we can multiply it later, we can do this by dividing the frame amount by 100, now we have 1% size of the frame. Now we can multiply this size by the current stamina amount, to get a size range from 0-100, and then set that offset to the bar. And that would net us the result, now we script the stamina system and add type checking so the stamina doesn’t go lower than 0, and boom, we’re done!

local Bar = script.Parent.Frame
local CurrentStamina = 100
local InitialBarSizeX = Bar.Size.X.Offset

while task.wait(0.1) do
	CurrentStamina = CurrentStamina - 1
	
	local NewSizeX = (InitialBarSizeX / 100)
	
local FrameSize = UDim2.fromOffset(NewSizeX * CurrentStamina, Bar.Size.Y.Offset)
	
	Bar.Size = FrameSize
	
	if CurrentStamina <= 0 then
		return
	end
	end
1 Like

Doesn’t work, I had to edit the script to fit my game (because I already have a stamina script) so i might have made a mistake. Here’s the edited code.

local Bar = script.Parent
local CurrentStamina = game.Players.LocalPlayer.CharacterAdded:Wait():WaitForChild("CharacterValues"):WaitForChild("Stamina")
local InitialBarSizeX = Bar.Size.X.Offset

while true do

	local NewSizeX = (InitialBarSizeX / 100)

	local FrameSize = UDim2.fromOffset(NewSizeX * CurrentStamina, Bar.Size.Y.Offset)

	Bar.Size = FrameSize

end

1 Like

I have no idea how your studio doesn’t crash in the while loop, add a task.wait(), also make sure your Value is between 0-100, otherwise it won’t work.

Its also recommended to use tweening for the size, as setting the size in a while loop isnt efficient.

1 Like
local Bar = script.Parent
local CurrentStamina = game.Players.LocalPlayer.CharacterAdded:Wait():WaitForChild("CharacterValues"):WaitForChild("Stamina")
local InitialBarSizeX = Bar.Size.X.Offset

while true do
	
	task.wait()

	local NewSizeX = (InitialBarSizeX / 100)

	local FrameSize = UDim2.fromOffset(NewSizeX * CurrentStamina, Bar.Size.Y.Offset)

	Bar.Size = FrameSize

end

Updated code, and still doesn’t work.

1 Like

Is your value between 0-100? Does your bar already have an offset? Can the script find the bar?

1 Like

For this kind of things you should use UDim2.fromScale, because with the Scale, there are only values between 0 and 1, where 1 would be covering the whole element or the whole screen.
In this case, the stamina value is a number between 0 - 100 so you just should set the frame X size (scale) to “NewSizeX”

1 Like
  1. Yes
  2. No
  3. Yes
    All of these things are fulfilled, yet it doesn’t work.
1 Like

Changed the code to this:

local Bar = script.Parent
local CurrentStamina = game.Players.LocalPlayer.CharacterAdded:Wait():WaitForChild("CharacterValues"):WaitForChild("Stamina")
local InitialBarSizeX = Bar.Size.X.Offset

while true do
	
	task.wait()

	local NewSizeX = (InitialBarSizeX / 100)

	local FrameSize = UDim2.fromScale(NewSizeX, Bar.Size.Y.Scale)

	Bar.Size = FrameSize

end

Doesn’t work.