Bar UI tween goes past frame

So my stamina bar goes outside the frame when it shouldn’t. I want the bar to stay inside the frame even when the Max Value increases. Here’s the script I’m using and a video of the problem happening, any help is appreciated! https://gyazo.com/9af2b48d54ce47b1174e14fd3666122a
here in the video the stamina value is decreasing by 10 out of 100 and the X scale of the bar is 0.90, the bar is only inside the frame when the X scale is 0.50 I hope this helps some.

	
local Player = game.Players.LocalPlayer

local Stamina = Player:WaitForChild("Stamina")
local MaxStamina = Player:WaitForChild("MaxStamina")
Player.Character:WaitForChild("Humanoid").Died:Connect(function()
   Stamina.Value = MaxStamina.Value
end)
local function refresh()--Function refresh
   if Stamina.Value < MaxStamina.Value then
   	local tweenamount = math.clamp(Stamina.Value,0, MaxStamina.Value)
   	
   	script.Parent:TweenSize(UDim2.new((1/MaxStamina.Value) * tweenamount,0,0.1,0),Enum.EasingDirection.In, "Linear",0.1)
   end
end



Stamina.Changed:Connect(function() 
   refresh() 
   if Stamina.Value > MaxStamina.Value then
   	Stamina.Value = MaxStamina.Value
   end
end)
MaxStamina.Changed:Connect(function()  
   refresh() 
end)


Have you tried using math.clamp on your Stamina.Value instead?

You might want to call refresh() after the if statement to see if Stamina is greater than MaxStamina in the section below.

Stamina.Changed:Connect(function() 
   refresh() 
   if Stamina.Value > MaxStamina.Value then
   	Stamina.Value = MaxStamina.Value
   end
end)

Also, you have a function at the bottom of the script that gets called when MaxStamina changes, but it is the same function as if Stamina changes.

Hello, so the if statement you mentioned is meant to be a fail safe if the value goes over the max. so there would be no point of putting refresh inside of it since every time the value changes I need the bar to update accordingly. Also could you elaborate on what you meant by the math.clamp part? like make Stamina.Value the max or something else?

I put a UiAspectRatio inside of it and that seemed to do the trick.

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