Making value only go 0-100

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
1 Like

You can use the math.clamp() function to make sure the value doesn’t exceed the limits.

EDIT: Hold on, I’m not sure if this function exists. You might want to make your own, like the post below says.

2 Likes
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:

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

hmmm well when i put that and modify in the script it make the stamina bar wacky…

normal :
Screen Shot 2020-05-22 at 9.15.40 AM

with math :
Screen Shot 2020-05-22 at 9.15.55 AM

code:

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)

local clamp = (function (val, min, max)
    return math.max(min, math.min(val, max))
end)

stmvalue.Changed:connect(function (val)
	stm:TweenSize(UDim2.new(val/stmvalue2.Value * .2,0,0.02,1),"In","Linear",1)
	stmvalue.Value = clamp(val, 0, 100)
end)

for i = -50, 150 do
	stmvalue.Value = i
	print(stmvalue.Value)
end

oh orange is stamina…

math.clamp() exists, source: math | Documentation - Roblox Creator Hub

2 Likes

That was an example, look at the bottom of my post - it’s updated.

Just rearrange the text in your script.

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.

1 Like

This would still update the bar to the incorrect value for 1 frame

Yours would too, but I don’t think that it really matters at the end of the day.

1 Like

when i did test that after i got my stamina low still go negative
Screen Shot 2020-05-22 at 9.20.40 AM

Paste code. You should be doing something similar to this:

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

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

1 Like

No it wouldn’t, the operation order of my example is:

  1. Clamp value
  2. 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.

2 Likes

oh you right you right, didn’t even think about the tween being there. thanks.

1 Like

thanks for help, it works, but one minor thing i notice is that the value actual real low but the bar not go to the negative

Unsure what you mean by this without further details or if you post a picture that can better describe your issue

1 Like


you can see the value actual very low but the bar not tween to it number (still work, just wonder why)

1 Like

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.

1 Like

what i meant was the value is at negatives, the script is “working” but actually not really, the value shouldnt go to negatives at all

1 Like

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'