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

Problems with Script

I'm having two problems with the following script. Whenever I run a command, this script runs as well. How do I get it to run once and stop? How can I increment the handout name? I want the handouts to called Dead Civilization #x. Here is the code: /** * Generates Names Civilization for Stars Without Numbers Revised * * Syntax: !DE option * * option determines which set of rollable tables will be used for Background, * equipment, and options such as Heritage or physical makeup */ var DE = DE || { output: [], listen: function () { on('chat:message', function (msg) { // Exit if not an api command if (msg.type != "api") { return; } else { DE.generate(msg, DE.printSheet, DE.save); sendChat('API', "/w gm <h6>Generating Dead Civilization</h6>"); } return; } }); }, generate: function (msg, outputCallback, saveCallback) { DE.id = msg.playerid; DE.name = "Dead Civilization #" + (findObjs({_type: "handout", name: "Dead Civilization"}).length + 1) DE.player = msg.who; DE.rollMD(); DE.rollMI(); DE.rollMM(); DE.rollTA(); DE.rollAS(); DE.rollSP(); DE.rollME(); DE.rollGM(); DE.rollBF(); DE.rollAA(); DE.rollMP(); DE.rollMA(); DE.rollLO(); DE.rollIN(); DE.rollLI(); DE.rollVO(); DE.rollSM(); DE.rollNH(); DE.rollCF(); DE.rollFE(); DE.rollUN(); DE.rollOB(); DE.rollWO(); DE.rollSR(); DE.rollTO(); if (typeof outputCallback === "function") { setTimeout(outputCallback, 5000, msg, saveCallback); } }, rollMD: function () { DE.MD = ''; sendChat("API", "/roll 1t[MadnessDetails]", function (result) { var content = JSON.parse(result[0].content); DE.MD = content.rolls[0].results[0].tableItem.name; }); }, rollMI: function () { DE.MI = ''; sendChat("API", "/roll 1t[MadnessIntensity]", function (result) { var content = JSON.parse(result[0].content); DE.MI = content.rolls[0].results[0].tableItem.name; }); }, rollMM: function () { DE.MM = ''; sendChat("API", "/roll 1t[MadnessMotivation]", function (result) { var content = JSON.parse(result[0].content); DE.MM = content.rolls[0].results[0].tableItem.name; }); }, rollTA: function () { DE.TA = ''; sendChat("API", "/roll 1t[TranshumanAberrations]", function (result) { var content = JSON.parse(result[0].content); DE.TA = content.rolls[0].results[0].tableItem.name; }); }, rollAS: function () { DE.AS = ''; sendChat("API", "/roll 1t[AlienSojourns]", function (result) { var content = JSON.parse(result[0].content); DE.AS = content.rolls[0].results[0].tableItem.name; }); }, rollSP: function () { DE.SP = ''; sendChat("API", "/roll 1t[SyntheticPurposes]", function (result) { var content = JSON.parse(result[0].content); DE.SP = content.rolls[0].results[0].tableItem.name; }); }, rollME: function () { DE.ME = ''; sendChat("API", "/roll 1t[MetadimensionalDeviancies]", function (result) { var content = JSON.parse(result[0].content); DE.ME = content.rolls[0].results[0].tableItem.name; }); }, rollGM: function () { DE.GM = ''; sendChat("API", "/roll 1t[GeneralMotif]", function (result) { var content = JSON.parse(result[0].content); DE.GM = content.rolls[0].results[0].tableItem.name; }); }, rollBF: function () { DE.BF = ''; sendChat("API", "/roll 1t[BodyFormat]", function (result) { var content = JSON.parse(result[0].content); DE.BF = content.rolls[0].results[0].tableItem.name; }); }, rollAA: function () { DE.AA = ''; sendChat("API", "/roll 1t[AnimalAspect]", function (result) { var content = JSON.parse(result[0].content); DE.AA = content.rolls[0].results[0].tableItem.name; }); }, rollMP: function () { DE.MP = ''; sendChat("API", "/roll 1t[Mouthparts]", function (result) { var content = JSON.parse(result[0].content); DE.MP = content.rolls[0].results[0].tableItem.name; }); }, rollMA: function () { DE.MA = ''; sendChat("API", "/roll 1t[Manipulators]", function (result) { var content = JSON.parse(result[0].content); DE.MA = content.rolls[0].results[0].tableItem.name; }); }, rollLO: function () { DE.LO = ''; sendChat("API", "/roll 1t[Locomotion]", function (result) { var content = JSON.parse(result[0].content); DE.LO = content.rolls[0].results[0].tableItem.name; }); }, rollIN: function () { DE.IN = ''; sendChat("API", "/roll 1t[Integument]", function (result) { var content = JSON.parse(result[0].content); DE.IN = content.rolls[0].results[0].tableItem.name; }); }, rollLI: function () { DE.LI = ''; sendChat("API", "/roll 1t[Limbs]", function (result) { var content = JSON.parse(result[0].content); DE.LI = content.rolls[0].results[0].tableItem.name; }); }, rollVO: function () { DE.VO = ''; sendChat("API", "/roll 1t[Vocalizations]", function (result) { var content = JSON.parse(result[0].content); DE.VO = content.rolls[0].results[0].tableItem.name; }); }, rollSM: function () { DE.SM = ''; sendChat("API", "/roll 1t[Smell]", function (result) { var content = JSON.parse(result[0].content); DE.SM = content.rolls[0].results[0].tableItem.name; }); }, rollNH: function () { DE.NH = ''; sendChat("API", "/roll 1t[NaturalHabitat]", function (result) { var content = JSON.parse(result[0].content); DE.NH = content.rolls[0].results[0].tableItem.name; }); }, rollCF: function () { DE.CF = ''; sendChat("API", "/roll 1t[ClothingForms]", function (result) { var content = JSON.parse(result[0].content); DE.CF = content.rolls[0].results[0].tableItem.name; }); }, rollFE: function () { DE.FE = ''; sendChat("API", "/roll 1t[FavoredElements]", function (result) { var content = JSON.parse(result[0].content); DE.FE = content.rolls[0].results[0].tableItem.name; }); }, rollUN: function () { DE.UN = ''; sendChat("API", "/roll 1t[UnusualNeed]", function (result) { var content = JSON.parse(result[0].content); DE.UN = content.rolls[0].results[0].tableItem.name; }); }, rollTO: function () { DE.TO = ''; sendChat("API", "/roll 1t[toOvercome]", function (result) { var content = JSON.parse(result[0].content); DE.TO = content.rolls[0].results[0].tableItem.name; }); }, rollOB: function () { DE.OB = ''; sendChat("API", "/roll 1t[OvercomeBy]", function (result) { var content = JSON.parse(result[0].content); DE.OB = content.rolls[0].results[0].tableItem.name; }); }, rollWO: function () { DE.WO = ''; sendChat("API", "/roll 1t[Worsened]", function (result) { var content = JSON.parse(result[0].content); DE.WO = content.rolls[0].results[0].tableItem.name; }); }, rollSR: function () { DE.SR = ''; sendChat("API", "/roll 1t[SeedofRuin]", function (result) { var content = JSON.parse(result[0].content); DE.SR = content.rolls[0].results[0].tableItem.name; }); }, printSheet: function (msg, saveCallback) { var styleLabel = "style='font-weight: bold; padding: 5px;'"; var styleVal = "style='padding: 5px;'"; DE.output['Madness'] = "<tr><td style='font-weight: bold; padding: 5px;'>MADNESS" + "</td></tr>"; DE.output['MD'] = "<tr><td style='font-weight: bold; padding: 5px;'>Details</td><td style='padding: 5px;'>" + DE.MD + "</td></tr>"; DE.output['MI'] = "<tr><td style='font-weight: bold; padding: 5px;'>Intensity</td><td style='padding: 5px;'>" + DE.MI + "</td></tr>"; DE.output['MM'] = "<tr><td style='font-weight: bold; padding: 5px;'>Motivation</td><td style='padding: 5px;'>" + DE.MM + "</td></tr>"; DE.output['Type'] = "<tr><td style='font-weight: bold; padding: 5px;'>TYPE" + "</td></tr>"; DE.output['TA'] = "<tr><td style='font-weight: bold; padding: 5px;'><p>Transhuman</td><td style='padding: 5px;'>" + DE.TA + "</td></tr>"; DE.output['AS'] = "<tr><td style='font-weight: bold; padding: 5px;'>Alien</td><td style='padding: 5px;'>" + DE.AS + "</td></tr>"; DE.output['SP'] = "<tr><td style='font-weight: bold; padding: 5px;'>Synthetic</td><td style='padding: 5px;'>" + DE.SP + "</td></tr>"; DE.output['ME'] = "<tr><td style='font-weight: bold; padding: 5px;'>Metadimensional</td><td style='padding: 5px;'>" + DE.ME + "</td></tr>"; DE.output['Shape'] = "<tr><td style='font-weight: bold; padding: 5px;'>SHAPE AND APPEARANCE" + "</td></tr>"; DE.output['GM'] = "<tr><td style='font-weight: bold; padding: 5px;'>General Motif</td><td style='padding: 5px;'>" + DE.GM + "</td></tr>"; DE.output['BF'] = "<tr><td style='font-weight: bold; padding: 5px;'>Body Format</td><td style='padding: 5px;'>" + DE.BF + "</td></tr>"; DE.output['AA'] = "<tr><td style='font-weight: bold; padding: 5px;'>Animal Aspect</td><td style='padding: 5px;'>" + DE.AA + "</td></tr>"; DE.output['MP'] = "<tr><td style='font-weight: bold; padding: 5px;'>Mouth Parts</td><td style='padding: 5px;'>" + DE.MP + "</td></tr>"; DE.output['MA'] = "<tr><td style='font-weight: bold; padding: 5px;'>Manipulators</td><td style='padding: 5px;'>" + DE.MA + "</td></tr>"; DE.output['LO'] = "<tr><td style='font-weight: bold; padding: 5px;'>Locomotion</td><td style='padding: 5px;'>" + DE.LO + "</td></tr>"; DE.output['IN'] = "<tr><td style='font-weight: bold; padding: 5px;'>Integument</td><td style='padding: 5px;'>" + DE.IN + "</td></tr>"; DE.output['LI'] = "<tr><td style='font-weight: bold; padding: 5px;'>Limbs</td><td style='padding: 5px;'>" + DE.LI + "</td></tr>"; DE.output['VO'] = "<tr><td style='font-weight: bold; padding: 5px;'>Vocalizations</td><td style='padding: 5px;'>" + DE.VO + "</td></tr>"; DE.output['SM'] = "<tr><td style='font-weight: bold; padding: 5px;'>Smell</td><td style='padding: 5px;'>" + DE.SM + "</td></tr>"; DE.output['NH'] = "<tr><td style='font-weight: bold; padding: 5px;'>Natural Habitat</td><td style='padding: 5px;'>" + DE.NH + "</td></tr>"; DE.output['CF'] = "<tr><td style='font-weight: bold; padding: 5px;'>Clothing Form</td><td style='padding: 5px;'>" + DE.CF + "</td></tr>"; DE.output['FE'] = "<tr><td style='font-weight: bold; padding: 5px;'>Favored Elements</td><td style='padding: 5px;'>" + DE.FE + "</td></tr>"; DE.output['UN'] = "<tr><td style='font-weight: bold; padding: 5px;'>Unusual Need</td><td style='padding: 5px;'>" + DE.UN + "</td></tr>"; DE.output['History'] = "<tr><td style='font-weight: bold; padding: 5px;'>HISTORY" + "</td></tr>"; DE.output['TO'] = "<tr><td style='font-weight: bold; padding: 5px;'>Obstacle to Overcome</td><td style='padding: 5px;'>" + DE.TO + "</td></tr>"; DE.output['OB'] = "<tr><td style='font-weight: bold; padding: 5px;'>Obstacle was Overcome By…</td><td style='padding: 5px;'>" + DE.OB + "</td></tr>"; DE.output['WO'] = "<tr><td style='font-weight: bold; padding: 5px;'>Seed Worsened Due To…</td><td style='padding: 5px;'>" + DE.WO + "</td></tr>"; DE.output['SR'] = "<tr><td style='font-weight: bold; padding: 5px;'>Seed of Ruin</td><td style='padding: 5px;'>" + DE.SR + "</td></tr>"; DEText1 = "<table font-size: 10px;'> <tbody>" + DE.output['Madness'] + DE.output['MD'] + DE.output['MI'] + DE.output['MM'] + "</tbody></table>"; DEText2 = "<table font-size: 10px;'> <tbody>" + DE.output['Type'] + DE.output['TA'] + DE.output['AS'] + DE.output['SP'] + DE.output['ME'] + "</tbody></table>"; DEText3 = "<table font-size: 10px;'> <tbody>" + DE.output['Shape'] + DE.output['GM'] + DE.output['BF'] + DE.output['AA'] + DE.output['MP'] + DE.output['MA'] + DE.output['LO'] + DE.output['IN'] + DE.output['LI'] + DE.output['VO'] + DE.output['SM'] + DE.output['NH'] + DE.output['CF'] + DE.output['FE'] + DE.output['UN'] + "</tbody></table>"; DEText4 = "<table font-size: 10px;'> <tbody>" + DE.output['History'] + DE.output['TO'] + DE.output['OB'] + DE.output['WO'] + DE.output['SR'] + "</tbody></table>"; DEText = DEText1 + "<p><p>" + DEText2 + "<p><p>" + DEText3 + "<p><p>" + DEText4 + "<p><p>"; sendChat(msg.who, "/w gm " + DEText); if (typeof saveCallback === "function") { saveCallback(); } }, save: function () { var handout = createObj("handout", { name: DE.name, }); handout.set('gmnotes', DEText); handoutId = handout.id; } }; on("ready", function () { DE.listen(); });
1652153018
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
You have the script setup to run on any API command. You need to check the content of the message to make sure that it's the proper command for the script. Now, that said, I'd really recommend doing all your rolls in a single sendChat, using inline rolls to roll all your tables at once. This will make it so you don't need as much asynchronous handling. You can also use Promises and async/await so that you don't need to use the 5 second timeout to wait for the roll results to return. Finally, I'd also recommend considering putting the table data directly in the script and rolling a random number to determine which entry to use. This will make the script much more portable.