# How would I use math.clamp?

Hi, as you’ve seen by the title I need help with the function Math.Clamp. I’ve read various posts and the api reference for the math.clamp, but I still don’t understand how to use it. If anyone could show me how to use it or explain how to use it, in a real situation in roblox studio then, it would greatly be appreciated!

6 Likes

Math.clamp is for “restricting” a value between 2 numbers
so…

``````math.clamp(5,1,3) -- 3 because 5 is bigger than 2
math.clamp(2,1,3) -- 2 because 2 is less than 3 but greater than 1
math.clamp(-10,1,3) -- 1 because -10 is less than 1
``````

Use cases would include if you want the player to be restricted to a certain space you can use math.clamp with the position to restrict the player movement.

3 Likes

when you want to limit a value between two numbers

1 Like

alright

``````math.clamp(-10,1,10)
``````

what would the middle number 1 be used for?

2 Likes

first value is the number you want to “clamp”, second value is min, third is max

2 Likes

math.clamp() restricts a certain number x between a min number and a max number.

``````local minNumber = 0
local maxNumber = 10
local x = 11
local n = math.clamp(x, minNumber, maxNumber)
print(n) -- will print 10 because x > 10``````
3 Likes

`math.clamp` could be the equivalent of this function:

``````local function clamp(x, min, max)
if x > max then return max;
elseif x < min then return min;
else return x; end
end

-- or alternatively
local function clamp(x, a, b)
return x > b and b or x < a and a or x;
end
``````
3 Likes

Alright, let me try testing so I can understand better

1 Like

Alright, thanks very much I understand it now!

3 Likes

The function takes `x`, `min` and `max` arguments and it returns the clamped value.

``````math.clamp(x, min, max)
``````

Let’s say we have `-10` for `x`, `0` for `min` and `10` for `max`.

``````local output = math.clamp(-10, 0, 10)
``````

if the input is `-10` then output will be `0` because `-10` is less than the minimum number `0`.
if the input is `20` then output will be `10` because `20` is greater than the maximum number `10`.
If the input number is lower than or equal to the maximum number and greater than or equal to the minimum number, the input number won’t change.

It can be used for scripting health bar, because we don’t want health bar to display values below 0 and greater than initial health value.

``````healthDisplay.Text = math.clamp(health, 0, 100)
``````
7 Likes

A gui has x scale from 0 to 1 and lets say you had max exp of 100,000 and starting from 0 exp… How can i use math.clamp to “scale” it down to 0 - 1 values(will be decimals) ?

It’s very basic math, you just have to divide min value by the max value. Here is an example:

``````100,000 / 100,000 = 1
50,000 / 100,000 = 0.5
25,000 / 100,000 = 0.25
0 / 100,000 = 0
``````

Therefore:

``````local MAX_EXP = 100_000
local currentEXP = 0
local value = if currentEXP == 0 then 0 else math.clamp(currentEXP, 0, MAX_EXP) / MAX_EXP
print(value)
``````

Note: There is an if statement to ensure that we don’t divide the 0.

Ik that lol… Im asking if math.clamp will do it for me

math.clamp ensures that the values will remain between the set lower and upper limit. It doesn’t do any division. Therefore, it will not scale down any values for you, it will only make sure that they don’t go out of the set bounds.

Oh so like if the number is less than the number it will return the min number and vice versa?

Yes, that’s basically what I have explained in my post, but it might not been clear enough.

Yea you might need to give more detail