MV Need help with a script call

Hi everyone,
so I need a script call that, if a switch is turned on, sets a random value of a variable between 0 and 3.
If that variable is 3 the script call is supposed to give the player an item.
Here's what I've got.

if ($gameSwitches.value(329)) {

$gameVariables.setValue(9, Math.randomInt(3))

if ($gameVariables.value(9) === 3) {

$gameParty.gainItem($dataItems[15], 1);}

}


Which sadly does nothing. What am I doing wrong?
Math.randomInt(3) returns 0, 1, or 2. It's a range of 3 possible integers, starting with 0. So the follow-up if statement will never be true, because the value can never be 3.

(I would generally recommend doing this with event commands. I assume you have a reason for doing it with scripting, though.)
Ah, I misunderstood that. Thanks, that was an easy fix.
Yeah, I know and it would have been far easier with event commands, but I need that with yanfly's item synthesis plugin. Basically I want to add a perk that has a 25% chance to return your crafting ingredient.
Which now works perfectly. Thanks! 🙂

Original thread: https://forums.rpgmakerweb.com/threads/need-help-with-a-script-call.175334/