Load Game by Script doesn't play bgm

Help~ I am frustrated as I have used below js to save&load the game, but the game doesn't play the bgm automatically

To save:
$gameSystem.onBeforeSave(); [U]//confirmed the current bgm has been saved to _bgmOnSave[/U] $gameSystem.setSavefileId(1); if (DataManager.saveGame(1)) StorageManager.cleanBackup(1);

Then closed the game, and re-open with below code to load the save

To load:

if (DataManager.loadGame(1)) { SceneManager._scene.fadeOutAll(); if ($gameSystem.versionId() !== $dataSystem.versionId) { $gamePlayer.reserveTransfer($gameMap.mapId(), $gamePlayer.x, $gamePlayer.y); $gamePlayer.requestMapReload(); } SceneManager.goto(Scene_Map); $gameSystem.onAfterLoad(); [U]//this function trying to replay the _bgmOnSave, but found the _bgmOnSave is null[/U] }

Does $gameSystem.onBeforeSave()/$gameSystem.onAfterLoad() pair works if I closed the game? If yes, then what is the issue?
Those are for MV, but MZ is different: it saves/loads asynchronously.

Try these script calls instead:
  • Save:
    JavaScript:
    $gameSystem.setSavefileId(1);
    $gameSystem.onBeforeSave();
    DataManager.saveGame(1);
  • Continue/Load:
    JavaScript:
    DataManager.loadGame(1)
      .then(() => {
        SoundManager.playLoad();
        SceneManager._scene.fadeOutAll();
        if ($gameSystem.versionId() !== $dataSystem.versionId) {
          const p = $gamePlayer;
          p.reserveTransfer($gameMap.mapId(), p.x, p.y, p.direction(), 0);
          p.requestMapReload();
        }
        SceneManager.goto(Scene_Map);
        $gameSystem.onAfterLoad();
      }).catch(() => SoundManager.playBuzzer());
Source: MV / MZ Script Call List. Note that there are checkboxes down the left-hand side of the main sheet to indicate which script calls apply to MV, MZ, or both.

[Edit: uncommented system SE calls in Continue/Load script call.]
Great, your script is working well for the bgm. Million thanks, Caethyril
Great, you're welcome! 👋

This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.

Original thread: https://forums.rpgmakerweb.com/threads/load-game-by-script-doesnt-play-bgm.155682/