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

2 Likes

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)

2 Likes

hi pal, i know im a little late but:

theres no MaxValue for variables cause you could also write it like this:
98.6.MaxValue
93.MaxValue

variables are just letters that describe something, in this case a number

if you want to have a MaxValue property, then you need to work with a table

local temp = {MaxValue = 98.6}
local counter = {MaxValue = 93}

in this example we descripe the item in the table as MaxValue

you can refer to this something like this

local number = 100
if number > counter.MaxValue then … end

hope this can answer your question

Hey there, it looks like you’re trying to explain what “math.clamp” does which is what the guy above explained. All good though,

To explain a little more:

math.clamp() is a function that adds a minimum and maximum value to a variable. However, you can simply do math.clamp() with a far-down min value.

Example:

local temp = 98.6
local counter = 93

local tempMaxValue = 98.6
local counterMaxValue = 93

temp = math.clamp(temp, -9999999, tempMaxValue)
counter = math.clamp(counter, -9999999, counterMaxValue)

-- Replace -9999999 with the lower limit that you want for the variables!

For more information, you can check out the official Roblox math.clamp documentation page here:

Use math.min.