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

Trusty old script seemingly broke

I've been using this script I found to handle shadowrun initiative, which is great - but it seems to have broken? I know jack all about this stuff myself, I just copy/pasted something I found. Any help figuring out why it doesn't work would be greatly appreciated! var iniobj = {     to: [],     check: '',     idx: 0 }; var inicheck = function() {     iniobj.po = iniobj.to;     iniobj.to = JSON.parse(Campaign().get('turnorder'));     iniobj.check = _.find(iniobj.to, function(obj) { return obj.custom == 'Initiative Pass'});     iniobj.idx = iniobj.to.indexOf(iniobj.check); }; var addini = function(oTurnOrder) {     oTurnOrder.unshift({         id: '-1',         pr: '1',         custom: 'Initiative Pass'     });     Campaign().set("turnorder", JSON.stringify(oTurnOrder)); }; on('chat:message', function(msg) {     // Check for "!ini" command     if(msg.type == 'api' && msg.content.indexOf('ini') !== -1) {         //Open Tracker if not open         if(Campaign().get('initiativepage') === false) {             Campaign().set('initiativepage', true);         };         //Setup iniobj         inicheck();         //Create "Initiative Pass" if it doesn't exist         if(iniobj.check === undefined){             addini(iniobj.to);         }         //Otherwise start a new turn         else{             iniobj.to.splice(iniobj.idx, 1);             addini(iniobj.to);         };     }; }); //Check for iniobj to make it to top of tracker on('change:campaign:turnorder', function() {     inicheck();     if(iniobj.idx === 0 && (_.isArray(iniobj.to) && _.isArray(iniobj.po) && !_.isEqual(iniobj.to[0],iniobj.po[0]) ) || (_.isArray(iniobj.to) && ! _.isArray(iniobj.po))){         iniobj.to[0].pr++;         for (var i = 1; i < iniobj.to.length; i++) {             iniobj.to[i].pr-=10;             iniobj.to[i].pr = iniobj.to[i].pr < 0 ? 0 : iniobj.to[i].pr;         };         Campaign().set("turnorder", JSON.stringify(iniobj.to));     }; });
1622808188
The Aaron
Roll20 Production Team
API Scripter
Can you go into detail into how it's failing?  Do your get an error (if so, can you paste it here), or incorrect behavior (if so, can you describe the expected vs actual behavior)?
A fair enough question! Shouldn't have been writing the question as I was dashing off to work :D I do indeed get an error message, and the script itself no longer does anything ("!ini" in chat should be generating an entry in the initiative which, once reached, decreases every initiative by 10 to a minimum of 0):
1622818202
timmaugh
Forum Champion
API Scripter
Do any characters have special characters ior punctuation n their names? (Also, none of your scripts will work once you get a sandbox error like this. You have to restart the sandbox...)
This is just in a test game, so there's no characters in it yet. That said, I've restarted the sandbox several times to no avail
1622822190

Edited 1622822237
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
If you haven't created an initiative turn before in the campaign, the turnorder property of the campaign is just "". This is not parsable by JSON.parse. You could either wrap that in a try/catch, or check the turn order for validity first. Also, I see a potential problem with your handling of the chat message. This line: if(msg.type == 'api' && msg.content.indexOf('ini') !== -1) { Wouldn't just trigger on !ini . It could trigger on !initiativeTracker  or !trackini or even !somecommand --initialize . I'd recommend changing to: if(msg.type == 'api' && msg.content.indexOf('!ini') === 0) {
Scott C. said:  You could either wrap that in a try/catch, or check the turn order for validity first Thank you! I've changed the line you suggested, but this bit here I have no idea what it means - as mentioned, my knowledge here is limited to the use of ctrl-c/v ;)
1623019526

Edited 1623019777
Pat
Pro
API Scripter
try{     // Put code here. If it fails, it won't stop execution.     iniobj.to = JSON.parse(Campaign().get('turnorder')); }catch(err){     // Code here if you want, when previous code fails.     iniobj.to = ""; // set some default value          log(err.message);     // log should be a default logging protocol to put the err.message to the roll20 console. }