MV Referencing Variables in Javascript/Script Calls

Hey! I'm trying to use this small script for randomizing shops that I found, but I want to reference a variable and not a set number. The script is this:

Code:
var id = 0, type = 0, min = 0, max = 0, inv = 0;

var array = [], temp = [], rand;
for(i = 0; i < inv; i++) {
do { rand = Math.floor(Math.random()*(max-min+1)+min);
} while (temp.indexOf(rand) > -1);
temp[i] = rand;
array[i] = [type, rand, 0];
}
if(Array.isArray($gameVariables.value(id)))
   var array = $gameVariables.value(id).concat(array);

You're meant to only change the 0s in the first line. What I want is to replace the max to the value of a variable that is added to with consumables in the game.

I tried

Code:
let giftsGiven = $gameVariables.value(88);
var id = 81, type = 0, min = 0, max = 'giftsGiven', inv = 0;

but it causes a crash as soon as you try to call the shop. I am a total novice when it comes to programming though and just pieced this attempt together reading a bunch of other scripts lol

Would really appreciate any advice! Thanks!
remove the quotes from giftsGiven inside here max = 'giftsGiven'
you set max to a string named 'giftsGiven', not to a number. your crash is probably due to the fact that you can't calculate with text.

simply use an
max = $gameVariables.value(88);
below the var max= 0 line from the original

EDIT: ninja'd
Wow, I could have sworn that I tried both of these methods, but apparently not! Works great, thank you both!!
you set max to a string named 'giftsGiven', not to a number. your crash is probably due to the fact that you can't calculate with text.

simply use an
max = $gameVariables.value(88);
below the var max= 0 line from the original

EDIT: ninja'd

I'm now having an issue where every time I call this script, it is adding to the variable rather than setting to it. So instead of calling this script and having the max being equal to the current value of variable 88, max is having the value of variable 88 added to it. Is there a way to zero it out first so that this functions the way I want it to?

EDIT: Nevermind, figured it out as soon as I hit post! Had to zero out the variable I was storing all this info in.
Is there a way to zero it out first so that this functions the way I want it to?
you need to use the operation "SET" (which overrides the old value) instead of the operation "ADD" (which adds to the old value)
you set max to a string named 'giftsGiven', not to a number. your crash is probably due to the fact that you can't calculate with text.

simply use an
max = $gameVariables.value(88);
below the var max= 0 line from the original

EDIT: ninja'd

Let's say I want the max to be equal to variable 88, but with a maximum of 50. Is there a clean way to handle that here, or should I work in a comment event to run before this script runs that finds the value of 88 and if it's higher than 50, sets it to 50?
I want the max to be equal to variable 88, but with a maximum of 50.
then you use
Code:
Math.max($gameVariables.value(88), 50)
instead
You might want to use Math.min instead if you want the lower of the two values.
JavaScript:
Math.min($gameVariables.value(88), 50)
Math.max would give you the bigger of the two values, setting a minimum value of 50 for the max variable, instead of a maximum value.
right you are 🤣
I make that mistake every time before realizing it when i try to use one or the other
You might want to use Math.min instead if you want the lower of the two values.
JavaScript:
Math.min($gameVariables.value(88), 50)
Math.max would give you the bigger of the two values, setting a minimum value of 50 for the max variable, instead of a maximum value.

Amazing, thank you! Oh this opens up so much for a ton of different stuff for this game.

Original thread: https://forums.rpgmakerweb.com/threads/referencing-variables-in-javascript-script-calls.176605/