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 repeating infinitly

I'm trying to do a script that, when i write !test on roll20 chat, checks if on a roll d100 make under 50 and in this case returns success and a number [d6*100] else it returns try again... but when i test this script it run infinitly (try again, try again....). Someone can help me understanding where i'm wrong? on('ready',()=>{   const d2 = () => randomInteger(2);   const d4 = () => randomInteger(4);   const d6 = () => randomInteger(6);   const d8 = () => randomInteger(8);   const d12 = () => randomInteger(12);   const d20 = () => randomInteger(20);   const d10 = () => randomInteger(10);   const d100 = () => randomInteger(100);        on('chat:message',(msg)=>     {    var AMR = 1     for (AMR; AMR > 0; AMR --)         {         var TesoroAMR =  d100()         if('api'===msg.type && msg.content.match(/^!test/) && TesoroAMR<50)             {             sendChat('', `Success ${d6()*100}`)             }          else   {                 sendChat('', `Try again`)    }}})})
1609932881

Edited 1609933517
GiGs
Pro
Sheet Author
API Scripter
I'm baffled by the for loop using AMR - that appears to be a counter that runs only once, in which case it isnt needed. I suspect the main problem is the if statement:  if('api'===msg.type && msg.content.match(/^!test/) && TesoroAMR<50) Note that the else for this statement should run on every message that is not an api, which is behaviour your definitely don't want. This line is why your script is running infinitely. Every time it prints the Try Again to chat, that line is put through the above if statement, and its not an API message, its just normal text, so it triggers the else statement and sends another Try Again to chat. I would suggest changing the construction to something like this on('ready', () => {     const die = (sides) => randomInteger(sides);     on('chat:message', (msg) => {         if ('api' === msg.type && msg.content.match(/^!test/)) {             const TesoroAMR = die(100);             if (TesoroAMR <= 50) {                 sendChat('', `Success ${die(6)*100}`);             } else {                 sendChat('', `Try again`);             }         }     }); }); This also replaces each of your d2, d4, d6, etc functions with a die(sides) function, which you can use for any size die. I havent tested this but give it a try.