[RMMZ] Saving and Loading the game through scripts commands

Hello everyone! (it's me again, still struggling on the same menu for my project 😅)
I'm trying to create a save/load menu using common events, and I'm having problems with the script command part.
I've tried using the commands suggested by forum users, and from the list of script commands, but I'm running into problems in both cases and have no idea how to solve them.
I'm using RPG Maker MZ 1.7.0.

Case 1)
Used commands
Save:
Code:
$gameSystem.setSavefileId(1);
$gameSystem.onBeforeSave();

Load:
Code:
DataManager.loadGame(1);

Issue:
Everything seems to be fine when I save, but the main character is invisible when I load the game.

Case 2)
Used commands
Save:
Code:
$gameSystem.setSavefileId(1);
$gameSystem.onBeforeSave();
await DataManager.saveGame(1);

Load:
Code:
await 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());

Issue:
If I try to save or load, I have an error message:

SyntaxError
await is only valid in async function​

Here's a quick link to the script call list.

I don't have enough knowledge in JS to know what is the issue. Can you help me?
Thank you for reading!
Case 2.
If I understand correctly this code is inside a "script" element in a Common Event, right?
Unless you have other lines of code or commands after these ones that manipulate save files, I think I would try removing the two "await" and see what happens.
The purpose of this word (await) is to manage synchonicity, but if you do not do anything "save related" immediately after these lines of code it should be ok.
Issue:
If I try to save or load, I have an error message:

If you are doing this within a script call inside an event then yes await is not available. There are two options you could try.

1) You could use an async IIFE, which should run the code once the script call is executed
JavaScript:
(async () => {
  $gameSystem.setSavefileId(1);
  $gameSystem.onBeforeSave();
  await DataManager.saveGame(1);
})();

2) You could remove the await part completely, and it should simply be treated as a promise

Which is how the autosave functionality works in MZ as you can see no need use of async at all.
JavaScript:
Scene_Base.prototype.executeAutosave = function() {
    $gameSystem.onBeforeSave();
    DataManager.saveGame(0)
        .then(() => this.onAutosaveSuccess())
        .catch(() => this.onAutosaveFailure());
};
"Case 2.
If I understand correctly this code is inside a "script" element in a Common Event, right?"
Shout, I wasn't really clear about that: Yes and no, actually.
The save game part will be included in the main menu in game, which will be a common event.
the load game will be a regular event, only accessible from the title screen map.

EDIT: I'm really sorry to be that bad at this 😅 So I tried to copy paste these codes lines, and either nothing happen when I ask the game to load a game (the character stays on the map of the title screen and the title screen event relaunch)
or it teleport the character on the corner of a map (which I believe is the one I saved the game on, according to the background) but the tiles don't show up.
@Ardonie Put together a small demo for you, in the demo the save game code is in a Common Event which is called by a regular event. Then the load game code is directly in the regular event. All of it appears to be working fine. Hopefeully this helps you with your game.

Dropbox: https://www.dropbox.com/scl/fi/xkq6...demo.zip?rlkey=831bho1in5p8jorhvbuu5tzhj&dl=0
thank you for the demo! I'll take a look at it and update you by tomorrow😉
😅 OK, that's on me. I admit I'm still learning about async stuff but I thought I tested that...will update the sheet sometime soon~

The await thing was my attempt to neatly solve the problem where the event does not wait for the operation to complete before processing additional commands, e.g.
◆Script:$gameSystem.setSavefileId(1); : :$gameSystem.onBeforeSave(); : :DataManager.saveGame(1) : : .then(() => alert("saved")) : : .catch(() => alert("error")); ◆Script:alert("this is the next command"); // this shows first
I believe this is mainly a concern for the Load Game script call, due to order of operations: stuff can change while waiting for data to load from disk.

It is possible to set up events such that they pause until the save succeeds (or fails), e.g. using a Loop and a (self-)switch:
Spoiler: example event setup
◆Control Self Switch:A = OFF ◆Script:const key = [this._mapId, this.eventId(), "A"]; : :$gameSystem.setSavefileId(1); : :$gameSystem.onBeforeSave(); : :DataManager.saveGame(1) : : .then(() => { alert("saved"); }) : : .catch(() => { alert("error"); }) : : .finally(() => { $gameSelfSwitches.setValue(key, true); }); ◆Loop ◆If:Self Switch A is ON ◆Break Loop ◆ :End ◆Wait:1 frame ◆ :Repeat Above ◆Script:alert("this is the next command");
It's not very elegant, but it works. Maybe someone else has a better suggestion. That said:
2) You could remove the await part completely, and it should simply be treated as a promise
It's usually safe enough to do this.
Aaand... I'm back from playtesting!
Sorry for the late response, it took me longer than I expected to get everything ready for a proper test.
I copy-pasted the codes from the demo to my project, adapting it slightly to fit the needs of the project and it works so far: I can save and load the game, the player is teleported on the proper map each time... I didn't try to load a game, go back to title and load another save yet, I guess I'll have to update you guys on that too.
I'm have to focus on the showing values part now.
Thank you for the demo by the way, it was really useful!

Original thread: https://forums.rpgmakerweb.com/threads/rmmz-saving-and-loading-the-game-through-scripts-commands.163813/