Roll20 uses cookies to improve your experience on our site. Cookies enable you to enjoy certain features, social sharing functionality, and tailor message and display ads to your interests on our site and others. They also help us understand how our site is being used. By continuing to use our site, you consent to our use of cookies. Update your cookie preferences .
×
Create a free account

Random HP roller for enemies

In case anyone else could use it I wrote a simple script to automatically roll different max HPs for selected enemies.  It uses the npc_hpformula attribute in the 5e character sheet.   Copy it into your scripts as a new script, then run it by using !RandomHP  with some tokens selected.  The token's bar1 will be set with the new HP.  If you screw up and don't pick a character with npc_hpformula filled out...like if you pick a player...then it'll skip that token and print out an error message.  It's only my second javaScript so sorry if its a little messy.  There are probably other scripts like it out there, but I wanted to learn, so I figured I'd make my own. on("ready",function(){         on("chat:message",function(msg){                  if(msg.type=="api" && msg.content.indexOf("!RandomHP")==0)         {         // for each selected token, see if they are in the stored info.           _.each(msg.selected,function(selectedItem) // for each selected item....             {             if (selectedItem._type == "graphic"){                                         let DoIt = 1;                 var tok = getObj("graphic",selectedItem._id);                 prev = JSON.parse(JSON.stringify(tok || {}));                 let character = getObj("character", tok.get("represents"));                 if(character == undefined) // if its not a character, skip.                     {                     sendChat("","Not a character. Skipping....");                     DoIt = 0;                     }                                  if(DoIt==1)                     {                     let CharName = character.get("name");                     let HPFormula = getAttrByName(character.id, "npc_hpformula");                     try                         {                         const NumDice = parseInt( HPFormula.split("d")[0] ) ;                         const DiceType = parseInt ( HPFormula.split("d")[1].split("+")[0] ) ;                         const Bonus = parseInt( HPFormula.split("+")[1] );                                                  let HP = Bonus;                                                  for (let i = 0; i < NumDice; i++) {                             var DiceResult = randomInteger(DiceType);                             HP += DiceResult;                             }                                                                    tok.set("bar1_value", HP); // WIP                             tok.set("bar1_max", HP); // WIP                             notifyObservers("change", tok, prev);                                                                                   } catch(err){sendChat("","Not an NPC. Skipping....");}                     }                                      }                                            })         }                     }); });
If you'd prefer a silent version that doesn't put anything in the chat, here it is. on("ready",function(){         on("chat:message",function(msg){                  if(msg.type=="api" && msg.content.indexOf("!Random_HP")==0)         {         // for each selected token, see if they are in the stored info.           _.each(msg.selected,function(selectedItem) // for each selected item....             {             if (selectedItem._type == "graphic"){                                         let DoIt = 1;                 var tok = getObj("graphic",selectedItem._id);                 let character = getObj("character", tok.get("represents"));                 if(character == undefined) // if its not a character, skip.                     {                     DoIt = 0;                     }                                  if(DoIt==1)                     {                                          let CharName = character.get("name");                     let HPFormula = getAttrByName(character.id, "npc_hpformula");                     try                         {                         const NumDice = parseInt( HPFormula.split("d")[0] ) ;                         const DiceType = parseInt ( HPFormula.split("d")[1].split("+")[0] ) ;                         const Bonus = parseInt( HPFormula.split("+")[1] );                                                  let HP = Bonus;                                                  for (let i = 0; i < NumDice; i++) {                             var DiceResult = randomInteger(DiceType);                             HP += DiceResult;                             }                         tok.set("bar1_value", HP);                          tok.set("bar1_max", HP);                          notifyObservers("change", tok, prev);                              } catch(err){DoIt=1;}                     }                                      }                                            })         }                     }); });
1686463447

Edited 1686463463
keithcurtis
Forum Champion
Marketplace Creator
API Scripter
Hi Antoine! Great first script! At first glance, it looks like you may want to call users' attention to the lines where you set  bar1_value . Not everyone assigns HP those to that bar, and may be unfamiliar with programming. Also, it's usually a good idea to let people know what sheet(s) your scripts work with. I'm guessing this is for the D&D 5th Edition by Roll20 Sheet?
Ah, good point.  If you want to change which bar it sets, change these lines: tok.set("bar1_value", HP) tok.set("bar1_max", HP) bar2 or bar3 are the other valid choices. And yeah, I am using the D&D 5th Edition by Roll20 Shee