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

[Script] FastGroupInitiative

1450027126

Edited 1450073680
Havoc
Sheet Author
API Scripter
Do you hate setting groups? You must make an encounter on the spot and don't have time to set all those nasty stats? Or you are just lazy and want the easy route? I've got something for you. An easy and fast group initiative roller. Just make a macro ("!tokeninit ?{modifier|0}"), select tokens, run it, set the initiative modifier and presto, you are done. API Script: function InitToken(mod, msg) {   try         { var turnorder;       if (Campaign().get("turnorder") == "") { turnorder = [];       } else turnorder = JSON.parse(Campaign().get("turnorder")); _.each(msg.selected, function(selected)  {                 var obj = getObj("graphic", selected._id);                 var who = "";                                 var currChar = getObj("character", obj.get("represents")) || "";                 var initString = " + " + mod;                                 if (currChar.length != 0)                  {                     who = currChar.get("name");                     if (currChar.get("controlledby") != "") return;                                                         }                  var roll = randomInteger(20) + parseInt(mod);                 var result = roll + "." + mod;                          turnorder.push({     id: selected._id,   pr: result,       custom: who       });             }); Campaign().set("turnorder", JSON.stringify(turnorder));         }         catch(err)         {             return;         }     };   on("chat:message", function(msg) { if( msg.type != 'api' ) return; var cmd = msg.content.toLowerCase().split(' '); if( cmd[0] == "!tokeninit" ) { InitToken( cmd[1], msg );   } });
1450051044
The Aaron
Pro
API Scripter
Not bad.   You can simplify your roll:     var roll = Math.floor((Math.random()*20)+1) + parseInt(mod); by using the built in randomInteger():     var roll = randomInteger(20) + parseInt(mod); Also, you might consider moving the turnorder changes to outside of the _.each().  as you have it, it will get the turnorder from campaign, parse it as JSON, add a turn, encode it as JSON, then set it for each selected token.  You really only need to do the add turn part inside the loop.
1450073768

Edited 1450077575
Havoc
Sheet Author
API Scripter
Thanks for the help. Fixed the code.
1450094641
The Aaron
Pro
API Scripter
Looking good!