Plugin to play SE automatically after variable change.

Hi, so, I am not really good at coding.
I am actually in the middle of creating a project and am making a relationship system.

That's why I decided to make a plugin that plays a sound effect upon variable change. I tried, but nothing works in the game.

I may be able to put a sound effect every time a variable changes in the editor.
But, I was wondering if there was an easier way to do so.

Is what I am asking for possible? Or should I stick with the editor for that purpose?

Here is my test plugin :
JavaScript:
/*:
* @plugindesc A plugin letting you put sound effects for variable changes.
* @author Sarry
*/

var check_change;

if (check_change != $gameVariables.value(1)) {
        $gameSystem.saveBgm();
    check_change = $gameVariables.value(1);
   
    }

(I put $gameSystem.saveBgm(); for testing.)

EDIT : So, I was able to do this using a parallel common event and it's working perfectly. But, I'm still interested in learning how to do this via javascript scripts/plugins.

Thanks.
Do you want the sound effect play when changing a specific variable Id? Or any variable?
What I recommend you to learn first is aliasing methods. This technique is similar to overriding, but instead of overriding, you extend a function. So we extend Game_Variables.set(id, value) in a way, that it plays a sound.

Code:
var alias = Game_Variables.prototype.setValue;

Game_Variables.prototype.setValue = function(id, value) {
    if (id == 1 // <-- your desired id to listen on
        && value != $gameVariables.value(1)) // <-- use this line if you want to play the SE only if the new value is different
    {
        // play sound
    }
    
    // call original function
    alias.call(this, id, value);
}
I don't know all commands by heart, so it may contain spelling errors.

Use this link to figure out how to play a SE 😉
Thank you for your fast answer. It was actually pretty easy. I'll see what I can do with this.
Thanks again.
@Eliaquim Hi and yes, I wanted to make it for at least 5 variables and some with more conditions like if the variable's value decreased, a specific se plays, if it increased, another would play, etc...

I managed to make this with a common event for two variables.
Don't worry too much about it, but in this case I think I'd go a little further. Ever been playing a game and suddenly a sound plays WAY louder than it should have? 👋 One way to help prevent that is to have the sound check happen only once per frame and use some kind of timer/counter to limit how frequently the sound can play, e.g. (untested):
Spoiler: Possible approach?
JavaScript:
/*:
* @plugindesc Hi! I'm a plugin!
*/

(function() {   // to keep seTime, TIME, and SE private to this plugin
'use strict';

  let seTime = 0;
  const TIME = 30;   // play up to once per this many frames
  const SE = { name: "Coin", volume: 20, pitch: 100, pan: 0 };

  // After normal update stuff, check if seTime is equal to TIME and play sound if so
  (function(alias) {
    Game_Map.prototype.update = function() {
      alias.apply(this, arguments);
      if (seTime > 0) {
        if (seTime === TIME) AudioManager.playSe(SE);
        --seTime;   // count down by 1
      }
    };
  })(Game_Map.prototype.update);

  // If seTime is zero when a variable's value is changed, set it equal to TIME
  (function(alias) {
    Game_Variables.prototype.setValue = function(variableId, value) {
      let preVal = this.value(variableId);
      alias.apply(this, arguments);
      if (!seTime && preVal !== this.value(variableId)) seTime = TIME;
    };
  })(Game_Variables.prototype.setValue);

})();

Similar thread, for posterity~
Thanks for your help.
I'll see what I can do.

Original thread: https://forums.rpgmakerweb.com/threads/plugin-to-play-se-automatically-after-variable-change.123762/