MV Coin Flip attack. Perform an additional attack per heads, end sequence at the first tails.

Hello, I am currently working on a Gambler class, and need to be able to loop a script call for the skill "Burnout". The skill relies on a coin flip, and for every heads performs an additional attack, with the sequence ending upon the first tails. This is the script I currently have:

Code:
<before eval>
if Math.random() < .5 )  {
var skill = $dataSkills[851];
entity = BattleManagerTBS.activeEntity();
BattleManagerTBS.forceAction(skill, entity);
}


else {

var skill = $dataSkills[47];
entity = BattleManagerTBS.activeEntity();
BattleManagerTBS.forceAction(skill, entity);

}


</before eval>

How would I repeat this sequence until reaching the else { cause? I would think there would be a loop or repeat function I could put at the bottom of the first if, but was unable to find anything with the terminology I used. Just a bunch of JS tutorials that honestly leave me with more questions than answers.

While I could loop the attack into itself based on Math.random(), this would mean the first attack would always hit if I am not mistaken. I suppose this could be a backup option but I think this should be relatively easy enough with the right JS.

Thank you in advance for your help.
Hello, I am currently working on a Gambler class, and need to be able to loop a script call for the skill "Burnout". The skill relies on a coin flip, and for every heads performs an additional attack, with the sequence ending upon the first tails. This is the script I currently have:

Code:
<before eval>
if Math.random() < .5 )  {
var skill = $dataSkills[851];
entity = BattleManagerTBS.activeEntity();
BattleManagerTBS.forceAction(skill, entity);
}


else {

var skill = $dataSkills[47];
entity = BattleManagerTBS.activeEntity();
BattleManagerTBS.forceAction(skill, entity);

}


</before eval>

How would I repeat this sequence until reaching the else { cause? I would think there would be a loop or repeat function I could put at the bottom of the first if, but was unable to find anything with the terminology I used. Just a bunch of JS tutorials that honestly leave me with more questions than answers.

While I could loop the attack into itself based on Math.random(), this would mean the first attack would always hit if I am not mistaken. I suppose this could be a backup option but I think this should be relatively easy enough with the right JS.

Thank you in advance for your help.
Hello. 👋
I don't use your battle system but why not just switch the if and else blocks? Then you can use the loop on the else block instead.
I am not sure I follow. If I switch the blocks wouldn't it just be the same result, a 50/50 coin toss? I am not sure how or where to input a loop condition. But either block would work for either purpose for me, as long as I can loop one of them until the other is processed.
I am not sure I follow. If I switch the blocks wouldn't it just be the same result, a 50/50 coin toss? I am not sure how or where to input a loop condition. But either block would work for either purpose for me, as long as I can loop one of them until the other is processed.
Oh silly me. 😩
I see. You want skill 47 to ALWAYS be at the end of the sequence, correct? My suggestion would cause the sequence to always START with skill 47 rather than END with skill 47. 😢

Why not try the code below?
I'm not sure how TBS works so forgive me for the redundant lines of code.
Spoiler: Code
JavaScript:
<before eval>
var skill;
if (Math.random() >= 0.5) {
  skill = $dataSkills[47];
  entity = BattleManagerTBS.activeEntity();
  BattleManagerTBS.forceAction(skill, entity);
} else {
  skill = $dataSkills[851];
  entity = BattleManagerTBS.activeEntity();
  BattleManagerTBS.forceAction(skill, entity);
  while (Math.random() < 0.5) {
    skill = $dataSkills[851];
    entity = BattleManagerTBS.activeEntity();
    BattleManagerTBS.forceAction(skill, entity);
  }
  skill = $dataSkills[47];
  entity = BattleManagerTBS.activeEntity();
  BattleManagerTBS.forceAction(skill, entity);
}
</before eval>
Though I must say this loop can theoretically go on forever so you may want to do something about it. You can break out of a loop by using the break statement. Below is an example that caps the "heads" to 10.
Spoiler: Code with Iteration Cap
JavaScript:
<before eval>
var max = 10;
var skill;
var i = 2;
if (Math.random() >= 0.5) {
  skill = $dataSkills[47];
  entity = BattleManagerTBS.activeEntity();
  BattleManagerTBS.forceAction(skill, entity);
} else {
  skill = $dataSkills[851];
  entity = BattleManagerTBS.activeEntity();
  BattleManagerTBS.forceAction(skill, entity);
  while (Math.random() < 0.5) {
    skill = $dataSkills[851];
    entity = BattleManagerTBS.activeEntity();
    BattleManagerTBS.forceAction(skill, entity);
    if (i > max) break;
    i++;
  }
  skill = $dataSkills[47];
  entity = BattleManagerTBS.activeEntity();
  BattleManagerTBS.forceAction(skill, entity);
}
</before eval>

Edit: I added lines (the three lines after else) to make sure if you get heads, skill 851 is guaranteed.
Oh I don't mind the loop going on for quite some time if the player is lucky enough, it would be quite hilarious in fact to end up clipping the final boss down in a single volley for instance.

I am going to try your code out now. I had something working out in a way but not completely, using the following below, but the lack of else conditions felt limiting. Your code looks a lot more polished and more what I was aiming for. Thank you kindly for the assistance. I will be right back to let you know how it works out.

Code:
<before eval>
while (Math.random() < .5 )  {

var skill = $dataSkills[851];
entity = BattleManagerTBS.activeEntity();
BattleManagerTBS.forceAction(skill, entity);
}
</before eval>

Ok, so it is kind of working, but I think due to the way I have skills linked with sequences after the fact, it is causing some issues with my dual wielding script. The dual weapon script basically unequips the offhand to attack with the main weapon, then equips what was in the offhand to the main hand to attack again, before removing both hands and equipping back to the original setup. This works fine and dandy, but it seems that when looped in either of these scripts the system is unable to save these items to a variable and re-equip them without causing an error, I presume which is from the script going faster than the engine can keep up.

Do you know of a way to add a delay in between each loop so as to have a more accurate sequence of attacks? Or even better, to wait until the initial sequence is completed before rolling again?

::SOLVED!!!::

Using this code and linking through 2 additional skills, for a total of 3 skills did the trick. The first skill contains the Math.random, and if successful links to the 2nd skill which perofmrs the first swing with the main handed weapon, which then links to the 3rd skill which is the second swing with the off hand weapon, which then links back to the initial Math.random skill for another roll.

Code:
<before eval>
var skill;
if (Math.random() >= 0.5) {
  skill = $dataSkills[48];
  entity = BattleManagerTBS.activeEntity();
  BattleManagerTBS.forceAction(skill, entity);
} else {
  skill = $dataSkills[37];
  entity = BattleManagerTBS.activeEntity();
  BattleManagerTBS.forceAction(skill, entity);
}
</before eval>

Thank you kindly Anatasius for the assistance and guidance!
::SOLVED!!!::

Using this code and linking through 2 additional skills, for a total of 3 skills did the trick. The first skill contains the Math.random, and if successful links to the 2nd skill which perofmrs the first swing with the main handed weapon, which then links to the 3rd skill which is the second swing with the off hand weapon, which then links back to the initial Math.random skill for another roll.

Code:
<before eval>
var skill;
if (Math.random() >= 0.5) {
  skill = $dataSkills[48];
  entity = BattleManagerTBS.activeEntity();
  BattleManagerTBS.forceAction(skill, entity);
} else {
  skill = $dataSkills[37];
  entity = BattleManagerTBS.activeEntity();
  BattleManagerTBS.forceAction(skill, entity);
}
</before eval>

Thank you kindly Anatasius for the assistance and guidance!
That's some skill! 👋
I wouldn't want to be the guy being poked by this skill that's for sure. 😅

This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.

Original thread: https://forums.rpgmakerweb.com/threads/coin-flip-attack-perform-an-additional-attack-per-heads-end-sequence-at-the-first-tails.146941/