MV Change the duration of each battler's state via the script

I have this code here:

JavaScript:
var _Add_State = Game_Battler.prototype.addState;
Game_Battler.prototype.addState = function (stateId) {
    _Add_State.call(this, stateId);
    //this.states(stateId)[0].minTurns += 2;
    //this.states(stateId).maxTurns += 5;
    console.log(this.states(stateId));
    console.log("STATE ID " + stateId)
    let regexMatch = /(?:<State\s* Duration\s* (\d+):\s* ([\-\+]\d+)>)/i
    console.log(this.traitObjects())
    this.traitObjects().forEach((element) => {
        let skillMeta = element.note.split(/[\r\n]+/);
        for (let a = 0; a < skillMeta.length; a++)
            if (regexMatch.test(skillMeta[a]))
                if (RegExp.$1 == stateId) {
                    for (let i = 0; i < this.states().length; i++)
                        if (this.states()[i].id == RegExp.$1) {
                            console.log("ID " + this.states()[i].id)
                            this.states()[i].minTurns += Number(RegExp.$2);
                            this.states()[i].maxTurns += Number(RegExp.$2)
                            this.resetStateCounts(stateId);
                        }
                }
    })
}

I want to change each battler's state duration based on the notes in their traits (armors, states, etc.) This code does do it, but it changes every battler affected by this state by every battler's note that affects this state. The turns become part of the class here rather than part of each specific battler object. How can I fix this?
That's because you're modifying the database records...usually a bad idea! 👋

Remaining turns for each state on a battler are stored on their _stateTurns object. You can modify the values directly, e.g. "+1 to the remaining turns for state ID 6 on this battler":

this._stateTurns[6] += 1;
Also, resetStateCounts just sets the turn count for that state equal to a random value between its min & max turns. You don't need to call that if you're modifying the turn count directly in addState. 👋

Original thread: https://forums.rpgmakerweb.com/threads/change-the-duration-of-each-battlers-state-via-the-script.156383/