Issue of TagChangeScripts when switching to the backup server

To prevent TagChangeScripts from triggering during startup or when saving a project, we added 'if not initialChange' at the top.
However, we noticed that when switching to the backup server, the scripts do not run correctly when the first time tag changed, but then they work fine afterward.
Right after switching to the backup server, the 'Last Execution' for all scripts shows as 'Never'. We suspect that when switching to the backup machine, the first trigger is being treated as 'initialChange'.
Is there anyway to avoid this issue?Thanks.

By the way, we are using version 8.1.37.

The whole point of running the events with the initialChange flag is to signal to your logic a "dead spot" in Ignition's knowledge of valid changes. The "dead spot" could be just a few milliseconds (project edits), or several seconds (redundancy failover), or minutes or hours (restarts and long shutdowns).

The value supplied when initialChange is true can be an actual change--you cannot just discard them. When recording based on changes, and when initial change is true, you must consult your recordings to determine what the last handled value was, then do your normal logic if a real change.

Thank you for your reply. We have tried the following method and it seems to be effective, but is there a better way?

There's no generic better way, as the technique needed to verify completed prior operations is entirely dependent on your application. Checking current versus previous values like shown only handles unexpected quality changes (from failover, presumably). It won't handle true changes coming in a dead spot.

Let's set aside the issue of dead spots for now.
During gateway startup or save, all scripts within Gateway Events are executed. Is there a way to trigger the same operations when the master switches to the backup?

I don't think so.