Stamina Bar goes past frame

how would i clamp the bars size instead?

the problem is, the bar wont deplete without the


this defeats the entire stamina bar system, the bar’s size wont change at all

Did you try it?

Lets say your current is 12, max is 30. 12/30 is 0.4
Lets say the size of the X background is 500 pix
0.4 * 500 is 200pix so now the size of your overlay bar would be (0,200,0,50)

If it doesn’t work then there are other issues with your code that we need to figure out

1 Like

i have, and


i tried what you said UDim2.new(0,current/max*StaminaBackground.AbsoluteSize.X, 0, 50)
here the is size of the gui:

try print("Current:",current,"Max:",max,"c/m:",current/max,"Background Size:", StaminaBackground.AbsoluteSize.X)

1 Like

printed using this line:

	--UDim2.new(0,current/max*StaminaBackground.AbsoluteSize.X, 0, 50)
	--players.LocalPlayer.PlayerGui.Stamina.Bar.Size = UDim2.new((math.clamp(current / max, 0, 1) * 0.3) - 0.3,298, 0, 50)
	players.LocalPlayer.PlayerGui.Stamina.Bar.Size = UDim2.new(((current/max)*0.3)-0.3, 298, 0, 50)

and,


that is with the bar going past

EDIT: and printing with your line of code

	UDim2.new(0,current/max*StaminaBackground.AbsoluteSize.X, 0, 50)
	--players.LocalPlayer.PlayerGui.Stamina.Bar.Size = UDim2.new((math.clamp(current / max, 0, 1) * 0.3) - 0.3,298, 0, 50)
	--players.LocalPlayer.PlayerGui.Stamina.Bar.Size = UDim2.new(((current/max)*0.3)-0.3, 298, 0, 50)


but i cant see the bars size change at all

why are you printing the stamina background size instead of the actual bar?

you need to set the bar size to this not just have it floating.

StaminaBar.Size = UDim2.new(0,current/max*StaminaBackground.AbsoluteSize.X, 0, 50)

--or 

players.LocalPlayer.PlayerGui.Stamina.Bar.Size = UDim2.new(0,current/max*StaminaBackground.AbsoluteSize.X, 0, 50)
2 Likes

how did i not notice that lol

1 Like

I’m assuming you want behavior like this:

Workspace layout:
workspace

Here is the code:

wait(4)

function sign(v) --http://lua-users.org/wiki/SimpleRound
	return (v >= 0 and 1) or -1
end
function round(v, bracket)
	bracket = bracket or 1
	return math.floor(v/bracket + sign(v) * 0.5) * bracket
end

while wait() do wait()	
	script.Parent.BarFrame.Bar.Size = UDim2.new((tick()%10)/10,0,1,0)
	script.Parent.Stat.Text = "Stamina: " .. round(tick()%10*10) .. "%\nSize: " .. tostring(script.Parent.BarFrame.Bar.Size)
end

--[[
	How the animation works:
	tick is the time as a number like: 65456.345645
	% is modulus (mod) operatior. Think division but it's the remainder.
		10/10 is 1 0/10, 10%10 is 0
		2/10 is 0 2/10, 2%10 is 2
		25/10 is 2 5/10, 25%10 is 5
		4/2 is 2 0/2, 4%2 is 0
		7/2 is 3 1/2, 7%2 is 1
		3.5/2 is 1 0.5/2, 3.5%2 is 0.5
	by %10 to tick we get an output of the range 0 to 9.999999...
	then divide that by 10 to get a range of 0 to 0.99999999......
--]]

What I’m doing is putting the bar I want to move inside of the background for it (barframe) then use the scaling to adjust it.

In your code:

UDim2.new(((current/max)*0.3)-0.3, 298, 0, 50)

You set the height correctly but the X is wrong. You get current/max which is good, the output range is 0 to 1. But then multiply by 0.3, output range is 0 to 0.3. Finally you -0.3, output range is 0 to -0.3.
Then you add 298 pixels on top of whatever negative size popped out.

If you want to keep your current gui layout you want to do something like:

UDim2.new(0,current/max*StaminaBackground.AbsoluteSize.X, 0, 50)