# How to make a variable have a maximum value

This is just a quick problem, but for some reason it gives me an error when putting this in.

``````local temp = 98.6
local counter = 93

temp.MaxValue = 98.6
counter.MaxValue = 93
``````

The error is:
Workspace.Zox_Dev.Tempreature:4: attempt to index number with ‘MaxValue’

Variables temp and counter are numbers, you tried to get .MaxValue of a number which does not exist. What are you trying to do exactly?

It seems they want to establish a MaxValue for their variables. But its something that is not built into Roblox itself. They need to program it themselves.

Maybe this is what you could be looking for?

``````local number = {Max = 7, Min = 1}

print(number.Max)
``````

prints the “max” number, which is 7 in this case.

or

``````local temp = 98.6
local counter = 93

print(math.max(temp,counter)) --98.6``````

Well when you do this the variable doesn’t have a “max value”, which I think is what they want.

You can do

``````local max = 10
local min = 1
local intvalue = intvaluepath
intvalue.Changed:Connect(function()
intvalue.Value = math.clamp(intvalue.Value, min, max)
end)
``````

U can do it with number values too

okay, So I am making it so that they go down and up, they can go down as much as they want but I don’t want them to ever go higher than the maxvalue

You can try my method. It works fine. You can do it with decimals and ints

you can use the example @luisgamercooI231fan sent above since you can make its min value as low as you want and limit the max

I’m not sure if they want a variable with variables min and max, or when a variable is changing to be set.

I am trying to make it so that the number can go down as much as it wants, but the temp cannot go above 98.6, and the counter cannot go above 93

Use math.clamp then.

local number = math.clamp(number, min, max)

1 Like