Writing script to set up talking SE based on Id of talking actor

I'm trying to write up a script as a common event, where based on the ID of the actor talking I get a corresponding SE (for testing purposes I set up the SE names as numbers).

JavaScript:
for (var x = 1; x < $gameActors._data.length; x++) {

    if ($gameMessage.speakerName() == $gameActors.actor(x).name()) {

       $gameVariables.setValue(8, x.toString());

       console.log("This is printed out!");

       AudioManager.playSe({name: $gameVariables.value(8), volume: 90, pitch: 100, pan: 0});

}   else {

       console.log("Failed!");

}

}

However I only get the "Failed!" prompt as a result. Do I need the .speakerName() call elsewhere or I have made some other mistake?
how many actors do you have? you mentioned seeing "Failed!" but realistically since this is looped, youre going to see success message at most once, and the failed message once for every other actor you have that isnt the one your looking for. are you sure you never saw trigger the success message?
how many actors do you have? you mentioned seeing "Failed!" but realistically since this is looped, youre going to see success message at most once, and the failed message once for every other actor you have that isnt the one your looking for. are you sure you never saw trigger the success message?

Yes, and by doing some troubleshooting I found out why: with console.log($gameMessage.speakerName()) I get a null result. I set up the script with a common event like this.

Screenshot.png


I've added a "$gameMessage.isBusy()" to see if something would change but nothing so far.
Shouldn't your common even be checking the message on parallel? In your screenshot you have trigger 'none', are you calling the common event between messages? In that case hasn't the message been terminated and the speaker name set to empty string?
Shouldn't your common even be checking the message on parallel? In your screenshot you have trigger 'none', are you calling the common event between messages? In that case hasn't the message been terminated and the speaker name set to empty string?
I've tried both checking it in parallel and creating a parallel event in the test map, but with no results.

I'm not calling it between messages, and for two reasons:
1) I'm using the RMMV-Ink plugin (compatible with MZ, as I successfully tested) to write the text in the Ink program, so I can't set it up there. Or rather, I can, but this just brings to me the second point
2) I specifically want to avoid calling this script every time a message appears, because I want the code to be dynamic. By setting up a specific sound for every actor (in my example I made their names equal to a number, but I can easily make their names the same as the actors'), I can make sure they have a different SE without coding it every time they talk.
Hmmm I put that script in parallel common event and then in an event with messages that have different names it seems to log correctly when the name matches
Hmmm I put that script in parallel common event and then in an event with messages that have different names it seems to log correctly when the name matches
Oh, apparently setting up the name with \N[n] was the problem. Luckily with the Ink Plugin I can just set the names up early. But yeah, it actually works!
Oh, I found out there's a tiny problem when the actors I call are not on the party, like NPCs I set up in the Actors section.

$gameActors._data doesn't seem to get them. Is there a command to acquire them?
Quick addendum, before someone suggests to set up the SE directly to the $gameMessage.speakerName(), it works but with 1 textbox delay.


JavaScript:
for (var x = 1; x < $gameActors._data.length; x++) {

if ($gameMessage.isBusy() && $gameMessage.speakerName() == $gameActors.actor(x).name()) {

$gameVariables.setValue(8, x.toString());

console.log("This is printed out!");

AudioManager.playSe({name: $gameVariables.value(8), volume: 90, pitch: 100, pan: 0});

}   else {

       $gameVariables.setValue(8, $gameMessage.speakerName());

       console.log("This is printed out!");

       AudioManager.playSe({name: $gameVariables.value(8), volume: 90, pitch: 100, pan: 0});


}
}

Original thread: https://forums.rpgmakerweb.com/threads/writing-script-to-set-up-talking-se-based-on-id-of-talking-actor.181883/