so im trying to make a stamina system, and the bar is based off of a value. the value is supposed to have a max of 100, and a minimum of 0, how can i make the value stop decreasing from 0 or stop increasing at 100?
local stm = script.Parent
local stmvalue = script.Parent.Stamina
local stmvalue2 = script.Parent.MaxStamina
stmvalue.Changed:Connect(function(value)
stm:TweenSize(UDim2.new(value/stmvalue2.Value * .2,0,0.02,1),"In","Linear",1)
end)
if stmvalue.Value >100 then
stmvalue.Value = 100
end
if stmvalue.Value <0 then
stmvalue.Value = 0
end
local clamp = (function (val, min, max)
return math.max(min, math.min(val, max))
end)
local NumberValue = Instance.new("NumberValue")
NumberValue.Changed:connect(function (val)
NumberValue.Value = clamp(val, 0, 100)
end)
for i = -50, 150 do
NumberValue.Value = i
print(NumberValue.Value) --> Will print 0 x51 times, 1 - 99, then 100 x51 times
end
imho though, it doesnât matter what your numbervalue is for your application it looks like? Your serverside script that alters this value should be checking whether it is allowed to remove that value from the numbervalue e.g.
if NumberValue.Value - Stamina_Amount < 0 then
-- don't perform the operation
else
-- it's > 0, so we can perform the operation and then remove the stamina
NumberValue.Value = NumberValue.Value - Stamina_Amount
end
Then in your .Changed event, if you really wanted to ensure this didnât happen, clamp the values. For your example:
stmvalue.Changed:Connect(function(value)
stm:TweenSize(UDim2.new(value/stmvalue2.Value * .2,0,0.02,1),"In","Linear",1)
--Put this stuff INSIDE the changed event:
if stmvalue.Value >100 then
stmvalue.Value = 100
end
if stmvalue.Value <0 then
stmvalue.Value = 0
end
end)
The script you had would only run the âifâ statements once, but with this script itâll run them every time the value changes, which is what you want.
Again, please read my reply, you shouldnât be getting a negative value in the first place anyway. The script that alters the stmvalue shouldnât be allowing it to get < 0, you need to add in an less/equal than operation to ensure the client has enough stamina to perform the action theyâd like to
No it wouldnât, the operation order of my example is:
Clamp value
Set the TweenSize
In yours, you set TweenSize first, and clamp afterwards - meaning that it would set to the original value and then clamp afterwards, before setting off the changed event once again to correct to the clamped value
Not being rude btw, just pointing out the order of operations as this is Scripting Support after all.
That looks like itâs just because you have a thick border size - reduce border size and that wonât appear, or you need to reduce the opacity of the GUI Object once itâs that low e.g.
local clamp = (function (val, min, max)
return math.max(min, math.min(val, max))
end)
stmvalue.Changed:Connect(function(value)
value = clamp(value, 0, 100)
stm:TweenSize(UDim2.new(value/stmvalue2.Value * .2,0,0.02,1),"In","Linear",1)
if value <= 0 then
-- use image transparency instead if it's that, but I have no clue what the architecture of your UIx is
stm.Visible = false
else
stm.Visible = true
end
end)
Obviously I donât know your UIx architecture, so you may prefer to use ImageTransparency = 0 / 1 if itâs an ImageLabel etc.
If you look at the code I provided for you above, it has a function called âclampâ. It can clamp a value between two others, i.e. it can make sure a number isnât above or below 2 other numbers
local old_value = math.random(-1, 1)*math.random() --> Just for testing, this is a random number between -1 and 1
local new_value = math.clamp(old_value, 0, 1) --> where 0 is the minimum value, and 1 is the maximum value; these can ofc be changed
print(old_value, "was clamped to", new_value)
--[!] The above would print something like: '-0.5 was clamped to 0'