Stamina Bar goes past frame

basically, the stamina bar goes a little past where it should:


(i drew a line where it should end)

here is the script:

function updateGui(current, max)

	if currentStamina >= 999 then
		tween:Play()
		tween2:Play()
		TextTween:Play()
	else
		StaminaBar.Transparency = 0
		StaminaBackground.Transparency = 0.6
		StaminaText.TextTransparency = 0
		
		tween:Cancel()
		tween2:Cancel()
		TextTween2:Cancel()
		TextTween2:Play()
	end


	players.LocalPlayer.PlayerGui.Stamina.Bar.Size = UDim2.new(((current/max)*0.3)-0.3, 298, 0, 50) --exact size of the bar
end
--the stamina system
userInputService.InputBegan:Connect(function(input)
	if hum.MoveDirection == Vector3.new() then return end
	if input.KeyCode == Enum.KeyCode.LeftShift then
		if currentStamina >= staminaCost then
			FullSprintAnim:Play()
			hum.WalkSpeed = sprintSpeed
			SprintFOVTween:Play()
		end
	
	elseif input.KeyCode == Enum.KeyCode.X then
		if currentStamina >= staminaCost2 then
			LightJogAnim:Play()
			hum.WalkSpeed = jogSpeed
			JogFOVTween:Play()
		end
	end
end)
-- Sprint Key Released
userInputService.InputEnded:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		SprintStoppedFOVTween:Play()
		FullSprintAnim:Stop()
		hum.WalkSpeed = walkSpeed
	elseif input.KeyCode == Enum.KeyCode.X then
		JogStoppedFOVTween:Play()
		LightJogAnim:Stop()
		hum.WalkSpeed = walkSpeed
	end
end)

runService.Heartbeat:Connect(function()
	if hum.WalkSpeed == sprintSpeed then
		if currentStamina >= staminaCost and hum.MoveDirection ~= Vector3.new() then
			currentStamina -= staminaCost
		else
			hum.WalkSpeed = walkSpeed
			FullSprintAnim:Stop()
		end 
	elseif hum.WalkSpeed == jogSpeed then
		if currentStamina >= staminaCost2 and hum.MoveDirection ~= Vector3.new() then
			currentStamina -= staminaCost2
		else
			hum.WalkSpeed = walkSpeed
			LightJogAnim:Stop()
		end
	else
		if currentStamina < maxStamina then 
			currentStamina += staminaRegen
		elseif currentStamina > maxStamina then
			currentStamina = maxStamina
		end
	end
	updateGui(currentStamina, maxStamina)
end)

This might be the problem, as when you’re setting the size of the stamina bar you subtracted 0.3 from the scale X. That causes it to go past its limit due to its X size being a negative number when your stamina gets too low.

2 Likes

ive tried that; whatever i change it to the bar is inaccurate -0.3 is the only number the fits the bars size, but ever number seems to have the same problem (this is 0):

even small increments like -0.31 makes the bar look like this:
so it seems -0.3 is the only accurate one

1 Like

@F0xBirdman can you post a pic of the workspace layout for this?

1 Like

Screen Shot 2021-05-06 at 8.19.36 AM
(starter gui)

Uhh. I didn’t read all of this but maybe if you have a value or something, you can do like

“if value.Value = 0 then” and your regenerating stamina and when it’s max “if value.Value = (yourmaxhere) then” then stop the moving stamina bar.

if you read the script you could see thats already done:

ill update the script i first posted, it wasnt full so nobody would take it

1 Like

Have you tried using math.clamp?

1 Like

actually, i have, i dont know how i could use it in this script

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

what is it clamping? the size of the ui?

Clamp the values so that current / max can never go above 1 or below 0, which would mean it’s extending past the normal bounds that you want.

1 Like

still is:


so current cant go below 0, and max cant go above 1?

Current / Max is the ratio used to find the length that the bar should be at any given moment. Clamping that value to 0 and 1 means that if Current is negative, or 0, the ratio is 0 and if Current is equal to max, or greater than max, the ratio is 1. For every other situation, the value will reside between 0 and 1. If the bar is still not the size you want it to be, then you should look towards adjusting the values you’re multiplying and adding onto the ratio value.

For instance, I used this code in a previous project to set the size of a hunger bar correctly.

UDim2.new((hungerValue / 100) * 0.2, 0, 0.75, 0)

hungerValue is set to the current value of hunger and is clamped from 0 to 100. At 0, the bar will have a size of 0. At 1, the bar will have a scale value of 0.2, which is the size I want it to be at max.

w h a t, clamping it seems to do nothing, maxStamina for example is 1000

but how does that change the bar from exceeding? sorry if im not understanding this.

Try parenting the bar to the background.
This way, you can simply set the bar’s size to UDim2.fromScale(current/max, 1)

If current stamina somehow falls below 0, or exceeds 1000, the ratio will either be below 0 or above 1. For instance, if current stamina is -5, then -5 / 1000 < 0 which would make the bar shift to the left, and if current stamina is 1050, then 1050 / 1000 > 1, which would make the bar shift further to the right.

Clamp the ratio to ensure it stays between 0 and 1, then multiply it by a scale value that you want the bar to be at max size.

So for some reason the all mighty moderation bot decided to flag my post and it’s been 12 hours…

Here’s a short version of my post:

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)

after printing the currentStamina, the stamina never goes below 0, this even applies when the updateGui function comments the code:

function updateGui(current, max)
	print(currentStamina)
	if currentStamina >= 999 then
		tween:Play()
		tween2:Play()
		TextTween:Play()
	else
		StaminaBar.Transparency = 0
		StaminaBackground.Transparency = 0.6
		StaminaText.TextTransparency = 0
		
		tween:Cancel()
		tween2:Cancel()
		TextTween2:Cancel()
		TextTween2:Play()
	end

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

im pretty sure it has nothing to do with the stamina going below, its the frame thats wrong

that defeats the main purpose of 0.3)-0.3, 298, 0, 50) right? how would you see the change in the bar