# Adding up to a maximum?

So, I have a number value that ranges from 0-20, and I’m trying to work out a way to make it go to 20 no matter what the value is.
So, Say it’s at 7,
I need a way to calulate how much to add to 7 to make it 20.

I can’t think of a better way to explain this, sorry if it’s too vague.

1 Like

Simple: max - amount = the number needed
20 - 7 = 13
7 + 13 = 20

1 Like

It works as registering the number in a print, But when I add it, it just comes out as 0.
Edit: Tried adding it in a print, registered as 20.

You might of made an error in your code, this is what it should look like:

``````local number = 7 -- whichever number you are using
NumberValue.Value = number + (20 - number) -- will always be 20
``````

I forgot the parentheses, That makes sense. Thanks for that.

1 Like

I have to ask, why do you need to do add to the number specifically? Why not just simply set the value to 20?

``````NumberValue.Value = 20
``````

If you want to cap adding to the value:

``````local numVal = --NumberValue
numVal.Changed:Connect(function()
if numVal.Value > 20 then
numVal.Value = 20
end
end)
``````

P.S. The parantheses should not affect the value at all, addition is associative

He might be lerping two values or something, it’s best not to ask.

Honestly, I’m not sure.
I’m the master at overthinking things I suppose.

Try stating what you need this for, give some context; we’ll help you solve it

What’s your use case? Why do you need to do this? Provided you don’t need to know the amount to reach the maximum from the current value and what you’re trying to do is add a pseudomaximum for a NumberValue, you can just use math.clamp to ensure that your number stays between a given minimum and maximum.

``````-- Making good use of this object here...
local numRange = NumberRange.new(0, 20)
local myCurrentNumber = 7

NumberValue.Value = math.clamp(myCurrentNumber, numRange.Min, numRange.Max)
``````