Using math.clamp() with external int values

Hello, I’m just looking for some help on how to properly use math.clamp on an external int value.

I’m working on a flashlight right now, and everything works fine except that the battery percentage can go negative. I know that I can use math.clamp to lock the value into 0,100 but I’m not sure exactly how to do it when referencing an external int value.

I’ve looked this up once or twice but didn’t see anything pertaining to use with actual IntValue’s in the explorer.

I tried to use math.clamp on my int value but it didn’t work. Here’s what I tried:

local b = script.Parent.Handle.Battery
b.Value = math.clamp(b.Value,0,100)

Though the battery still went negative and I didn’t have any errors thrown up.

Extra:
My flashlight drains the battery through an external function that’s called everytime you turn it on. It all works fine but goes negative because the intValue isn’t clamped to minimum 0 maximum 100. I know that I could just check if it’s equal to 0 and set it to 0 everytime it sees that it’s zero but that seems like a rather hacky solution especially with solutions like math.clamp around.

For clamp to function properly, you should use it each time you assign a new value to your variable.

It won’t ‘lock’ the variable preventing negative numbers from being assigned, it just makes sure the input argument is within range of the specified bounds each time it’s called.

local x = math.clamp(-2, 0, 1) --> 0

x  = 5 --> the value 5 is now stored in x

Okay, so inside my battery draining function before my b.Value -= 1 I should clamp b.Value?

Yup exactly.

clamp is merely just a shorthand of writing the following:

local function clampValue(value, min, max)
if value < min then
return min
elseif value > max then
return max
else
return value
end
end
--//ignore poor indentation, roblox still hasn't fixed this
local clamped = clampValue(-50, 0, 100)

This seems to have mostly fixed it, now It can only drop to -1. Sorry for my incompetence on the math functions I try to avoid them because I’m bad at math lol.

What code did you try?

I’m guessing you didn’t put the whole expression for your initial value through to the clamp call.

Here’s what I mean:

local maxBattery = 100
local decrementRate = 1

local newBatteryValue = math.clamp(battery.Value - decrementRate, 0, maxBattery)
battery.Value = newBatteryValue
2 Likes

I Changed it to

b.Value = math.clamp(b.Value,0,100)
b.Value -= 1

But what you’ve sent makes much more sense still trying to wrap my head around it all the way lol going to edit my code to be more similar to the above real quick.

Basically, clamp is a function which returns a value. It takes 3 parameters (or values) in which you’re supplying b.Value which is the value to clamp, followed by the min value, and the max value.

It clamps the value of b, and then returns it. In the first line of your example, the clamped value is assigned as the new value of b. (although the original value of b was used)

In the second line, you decrement the value of b by 1, but you don’t call clamp when you do this, so it can still end up being negative.

1 Like

clamp is a relatively simple function to understand, you pass it three values, the value you want to clamp, the lower limit and the upper limit, in that order. The function then clamps the value between the two limits, if the value is already within the specified limits then the value itself is returned, if the value is below the lower limit then the lower limit is returned and if the value is above the upper limit then the upper limit is returned.