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 .
×

Calls on one script but gets result from two

Dear all,  I've encountered a strange problem that I'm hoping that someone could help me with. To start with, I have a very basic knowledge of coding so when constructing these, I've borrowed code from others and mostly worked by trial and error. Anyway, I have two scripts, Script#1 and Script#2. Script#1 is a placeholder for a larger rollable table, but works just the way I want it to. One uses it by writing !test X, where X is a number, for instance !test 1 !test 2 etc. The second script is to roll exploding d6. One uses it by writing !ob X+Y , which is rolled as Xd6+Y. The problem is that when you do, it works but it also presents the results for the first script using the variable X.  I've got no idea why script 2 presents the result from both script 1 and 2. Any help would be deeply appreciated.  SCRIPT 1, name ‘test’ on("chat:message", function(Roll) {     if (Roll.type != "api") {         return;     }     var content = Roll.content;     var CritRoll = parseInt(content.split(" ")[1]); if (CritRoll == 1)     {sendChat("Crit table 1", "A one");     } else if (CritRoll == 2)     {sendChat("Crit table 1", "A two");     } else if (CritRoll == 3)     {sendChat("Crit table 1", "A three");     } else     {sendChat("Crit table 1", "Higher")     } }); SCRIPT 2, name ‘ob’ on("chat:message", function(msg) {     if (msg.type != "api") {         return;     }     var message = msg.content;     var command = message.split(" ")[0];     var numrolls = parseInt(message.split(" ")[1]);     var outroll = "ob" + message.split(" ")[1] + ": ";     var plus = 0;     var times = 0;     if(message.search(/\+/g)!=-1){         plus = parseInt(message.split("+")[1]);     }     else if(message.search(/\*/g)!=-1){         times = parseInt(message.split("*")[1]);     }     if (command == "!ob") {         var count = 0;         var total = 0;         var roll = 0;         var first = true;         var output = "" + outroll + "" ;         while (count < numrolls) {             roll = randomInteger(6);             if (roll === 6) {                 if(first == true){                     output = output + "[6]";                     first = false;                     numrolls = numrolls + 2;                 }                 else{                     output = output + ",[6]";                     numrolls = numrolls + 2;                 }             } else {                 total += roll;                 if(first == true){                     output = output + roll;                     first = false;                 }                 else{                     output = output + "," + roll;                 }             }             count++;         }         if(plus > 0 && times == 0){             sendChat(msg.who, output + " = " + total + "+" + plus + " Total: " + (total+plus) + "");         }         else if(plus == 0 && times > 0){             sendChat(msg.who, output + " = " + total + "*" + times + " Total: " + (total*times) + "");         }         else{             sendChat(msg.who, output + " Total: " + (total) + "");         }     } });
1681826289
timmaugh
Forum Champion
API Scripter
Hey, Johan... Script 2 isn't your problem. You're getting results from both scripts because #1 isn't gated to keep it from responding to every message that comes through. Script #1 just says, "If it's an API message, take action." Most times you will see a test for the command line handle at the same time that the message type is tested to equal 'api'. That might look like this: if (msg.type !== 'api' || !/^!test\b/.test(msg.content))  return; That uses a regex tuned to the handle you said your first script would use: "test". Another option people have used is to use indexOf: if (msg.type !== 'api' || msg.content.indexOf('!test') !== 0)  return; ...or even startsWith: if (msg.type !== 'api' || !msg.content.startsWith('!test'))  return; Once you've introduced a limiter like that to your script, the message must both qualify as an 'api' message, AND it must include the trigger you're looking for. Your second script does do a bit of gatekeeping, but it does it very late: if (command=="!ob")... Moving this earlier (combining it with the check for the message type being 'api', as I just showed for script #1) will save processing on messages that don't need to be parsed, and it will clean up your code a bit. Also, there are some general suggestions I would make as to your code, but I'll keep them to myself unless you're looking for input. For now, the above should solve the issue you asked about.
Thank you, that solved my problem. Much appreciated!