Random state at max tp

I cannot figure out how to change these notetags into something that will
  • Give a random state out of a group of states to the actor when they reach max tp
  • Reduce said actors tp to zero
  • Remove the previous state and add a new one when they reach max tp again
here is the thread with the notetag in question - [SOLVED] Give a Random State
--------------------------------------------------------------------------------------------
edit: So I managed it somewhat, but it doesn't always add replace the state, sometimes it just removes it and leaves them with no state at all.

Spoiler: code
Code:
<Custom Turn Start Effect>
if (user.tp>=100) {
user.removeState(32);
user.removeState(33);
user.removeState(34);
user.removeState(35);
user.startAnimation(123);
var rr = Math.floor(Math.random() * 4);
if(rr==1){
user.addState(32);
}else if(rr==2){
user.addState(33);
}else if(rr==3){
user.addState(34);
}else{
user.addState(35);
}
user.gainTp(-100);
}
</Custom Turn Start Effect>

also I can't figure out how to add a wait time for the animation to finish
This one made it work
Spoiler
Code:
<Custom Turn Start Effect>
if (user.tp>=100) {
user.startAnimation(124);
var rr = Math.floor(Math.random() * 4) + 1;
switch(rr){
case 1:
user.addState(32);user.removeState(33);user.removeState(34);user.removeState(35);
break;
case 2:
user.addState(33);user.removeState(32);user.removeState(34);user.removeState(35);
break;
case 3:
user.addState(34);user.removeState(33);user.removeState(32);user.removeState(35);
break;
case 4:
user.addState(35);user.removeState(33);user.removeState(34);user.removeState(32);
}
user.gainTp(-100);
}
</Custom Turn Start Effect>

Original thread: https://forums.rpgmakerweb.com/threads/random-state-at-max-tp.124813/