MZ day night system transitional fade script help

In my day night system I have script that transitions in NPCs at certain times but I've run into an issue where they just pop away instantly and the new ones instantly appear. It works like it coded but it looks bad and I've been trying to get them to kind of do a fade out over five seconds and the new ones fade in but it's not working. When I have been able to get it to work they don't fade out consistently. First time they'll fade out slowly the next time it'll be almost instant and none of them are in sync with each other. Can anyone help me?


* To Use:

* 1. Add a "Script" event command.

* 2. Paste this code into the Script command.

* 3. Customize the settings within the CONFIGURATION section.

*/



// *** CONFIGURATION ***

const targetEventIds = [19, 20, 21, 22, 23,]; // IDs of the target events (NPCs)

const visibleStartHour = 10; // Hour when NPCs become visible (24-hour format)

const visibleEndHour = 18; // Hour when NPCs become invisible (24-hour format)

const switchId = 21; // ID of switch to set when events are visible (optional)

// *** END CONFIGURATION ***



if ($gameMap) {

const currentHour = TimeManager.getHour();

const isVisible = (currentHour >= visibleStartHour && currentHour < visibleEndHour);



targetEventIds.forEach(eventId => {

const event = $gameMap.event(eventId);



if (event) {

event.setOpacity(isVisible ? 255 : 0);

if (switchId > 0) $gameSwitches.setValue(switchId, isVisible); // Corrected inversion

event.setThrough(!isVisible); // Make it not collidable with the player when it's invisible

} else {

console.warn(`Event with ID ${eventId} not found.`);

}

});

} else {

console.warn("This script must be run on a map scene.");

}

Original thread: https://forums.rpgmakerweb.com/threads/day-night-system-transitional-fade-script-help.176424/