Hi, I am currently doing a Vertical Stamina Bar But i’ve ran into a problem , the Stamina bar resizes out of the limits, And it’s not intended to do this
The script that resizes the bar :
local function update()
if humanoid.MoveDirection.Magnitude > 0 and running and not crouching and stamina > 0 then
humanoid.WalkSpeed = 25
playAnimation("run")
regenTime = time()
stamina -= 0.5
else
if not crouching then
humanoid.WalkSpeed = game.StarterPlayer.CharacterWalkSpeed
stopAnimation("run")
else
stopAnimation("run")
humanoid.WalkSpeed = 12
end
if stamina < maxStamina then
if (time() - regenTime) > 5 then
stamina += 0.25
end
end
end
_G.STAMINA = stamina
end
task.spawn(function()
while task.wait() do
update()
end
end)
runService.RenderStepped:Connect(function()
--healthBar.Size = healthBar.Size:Lerp(UDim2.fromScale(humanoid.Health / humanoid.MaxHealth,1),0.25)
--energyBar.Size = energyBar.Size:Lerp(UDim2.fromScale(stamina / maxStamina,1),0.25)
energyBar.Size = energyBar.Size:Lerp(UDim2.fromScale(0.89 , stamina / maxStamina),0.25) -- this is it
end)
userInput.InputBegan:Connect(inputBegan)
userInput.InputEnded:Connect(inputEnded)
is there any way to resize the bar To not go out of limits?, but also displaying the correct amount of Stamina?, that would be 0 of 100
you can use math.clamp(x, minvalue, maxvalue) this way it wont exceed what maxvalue is, or go under minvalue
edit: I did not notice your remoteevent doing this.
local currentValue = stamina
local Formula = math.clamp(currentValue/maxStamina, 0, 1)
energyBar.Size = energyBar.Size:Lerp(UDim2.new(1, 0, Formula),0.25)
In the Explorer, is the moving part of the Stamina bar placed directly into the Frame / ImageLabel that makes up the background image of the Stamina bar? If so, do the boundaries of that background object match up with its visuals (meaning that when you select the background, the outline that appears are at the actual edges of the image / frame) or does the outline extend further than where the visible part of the image / frame stops?
If the outline does not match up with the visual boundaries of the image / frame that the moving part of the Stamina bar was placed into, that is likely to be causing the issue, since a Y Axis Scale value of 1 would be resizing it based on where the outline is rather than where the frame / image visually appears to stop.
As a quick side note, UDim2.fromScale() only requires you to input two numbers; the new X Axis and Y Axis scale, which means that the 0.25 at the end would not be necessary. After running some quick tests in Roblox Studio, I don’t think that extra number would be causing it to be resized to an unintended value, but I wanted to highlight it just in case.
Disregard this; I misread the code! Thanks to @nennocyte for correcting me:
Ah, this is what I had suspected. If you select the StaminaBar image, it is likely that its boundaries extend much further up or down than what it looks like.
In order to account for that difference if you cannot or don’t want to crop the StaminaBar, the Script will need to treat it as if the maximum scale of the Y Axis is 0.819 instead of 1.
In order to do that, you can have the Script multiply Stamina / MaxStamina by 0.819 so it’ll set it to a scale that is proportional to that.
As a result, here is what that line of code from the original post could be revised to:
And it finally worked as intended, I really appreciate both of your help, I will mark @nennocyte post As the solution, Because i wouldn’t have noticed if it wasn’t for it, But don’t worry, As i said, thank you both for you help
that code will make the stamina display 80 stamina as 100, i recommend either resizing the UI in studio itself (by hand, and not in playtest) to match up the hp bar or trying @StrongBigeMan9 s solution, though it’s kinda tacky and not really the best practice. (would still work obv)