How to set a maximum amount of a variable?

I'm trying to design a custom resource through basic Javascript that relies on using a custom variable. I can't quite figure out how to put a limit on the amount you can generate though.
I've tried
Code:
var Skill = $gameVariables.value(1).clamp(0,100);
but that runs into an issue where if the amount generated goes over 100 (i.e. Skill is at 99 but if 2 is added it goes up to 101) it stays at that value unless it's depleted.

Is there a simple solution to this?
There should be a check of the current value of variable 2 where ever you are adding or subtracting to the value. By the time a value is added, it is a bit too late for the clamp function.
There should be a check of the current value of variable 2 where ever you are adding or subtracting to the value. By the time a value is added, it is a bit too late for the clamp function.
Sorry to ask, but could you give me an example? I can't quite work it out myself.
Perhaps something along the lines of the following :

Code:
// When the Variable is being set ...
// Add or Subtract from Variable ...
tmpValue = $gameVariables.value(1) - 150;
// Check and Clamp value ...
tmpValue = tmpValue.clamp(0, 100);
// Set Variable with Clamp'ed value ...
$gameVariables.setValue(1, tmpValue);

// When you are retrieving the Variable ...
var skill = $gameVariables.value(1);
// The value is already Clamp'ed and will never go over 100 or go below 0.
Perhaps something along the lines of the following :

Works absolute perfect!
Thank you so much. You've saved me a sleepless night.
Also, Yanfly just released or is going to release soon a plugin to give variables ranges. If I recall correctly.

Original thread: https://forums.rpgmakerweb.com/threads/how-to-set-a-maximum-amount-of-a-variable.113045/