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 Cards

I'm not much of a coder, so I had AI write my code for Script Cards. I read the update on AI, and didn't see a prohibition about using it for macros, but if this isn't allowed, please let me know. on("ready", function() {   // Define the names of your rollable tables   const table1Name = "Roomshape";   const table2Name = "Directions";   const table3Name = "Cavern-Features";   const table4Name = "Investigation-Results";   // Retrieve the list of items from each rollable table   const table1 = findObjs({ type: "rollabletable", name: table1Name })[0];   const table2 = findObjs({ type: "rollabletable", name: table2Name })[0];   const table3 = findObjs({ type: "rollabletable", name: table3Name })[0];   const table4 = findObjs({ type: "rollabletable", name: table4Name })[0];   // Create arrays to keep track of used results for each table   let usedResults1 = [];   let usedResults2 = [];   let usedResults3 = [];   let usedResults4 = [];   // Function to roll on a rollable table and retrieve a result   function rollOnTable(table, usedResults) {     // Retrieve the list of items from the rollable table     const tableItems = findObjs({ type: "tableitem", _rollabletableid: table.get("_id") });     // Filter out items that haven't been used yet     const availableItems = tableItems.filter(item => !usedResults.includes(item.get("name")));     // If all items have been used, reset the usedResults array     if (availableItems.length === 0) {       usedResults = [];     }     // Randomly select an item from the available items     const randomIndex = Math.floor(Math.random() * availableItems.length);     const selectedItem = availableItems[randomIndex];     // Add the selected item to the usedResults array     usedResults.push(selectedItem.get("name"));     // Return the selected item     return selectedItem.get("name");   }   // Roll on each table and retrieve a result   const result1 = rollOnTable(table1, usedResults1);   const result2 = rollOnTable(table2, usedResults2);   const result3 = rollOnTable(table3, usedResults3);   const result4 = rollOnTable(table4, usedResults4);   // Output the results in the Roll20 chat   sendChat("Roll20", `&{template:default} {{Roomshape: ${result1}}} {{Directions: ${result2}}} {{Cavern-Features: ${result3}}} {{Investigation-Results: ${result4}}}`); }); This was my output on("ready", function() { // Define the names of your rollable tables const table1Name = "Roomshape"; const table2Name = "Directions"; const table3Name = "Cavern-Features"; const table4Name = "Investigation-Results";  (GM): // Retrieve the list of items from each rollable table const table1 = findObjs({ type: "rollabletable", name: table1Name })[0]; const table2 = findObjs({ type: "rollabletable", name: table2Name })[0]; const table3 = findObjs({ type: "rollabletable", name: table3Name })[0]; const table4 = findObjs({ type: "rollabletable", name: table4Name })[0]; // Create arrays to keep track of used results for each table  (GM): let usedResults1 = []; let usedResults2 = []; let usedResults3 = []; let usedResults4 = []; // Function to roll on a rollable table and retrieve a result function rollOnTable(table, usedResults) {  (GM): // Retrieve the list of items from the rollable table const tableItems = findObjs({ type: "tableitem", _rollabletableid: table.get("_id") }); // Filter out items that haven't been used yet const availableItems = tableItems.filter(item => !usedResults.includes(item.get("name"))); // If all items have been used, reset the usedResults array if (availableItems.length === 0) {  (GM): usedResults = []; } // Randomly select an item from the available items const randomIndex = Math.floor(Math.random() * availableItems.length); const selectedItem = availableItems[randomIndex]; // Add the selected item to the usedResults array  (GM): usedResults.push(selectedItem.get("name")); // Return the selected item return selectedItem.get("name"); } // Roll on each table and retrieve a result const result1 = rollOnTable(table1, usedResults1);  (GM): const result2 = rollOnTable(table2, usedResults2); const result3 = rollOnTable(table3, usedResults3); const result4 = rollOnTable(table4, usedResults4); // Output the results in the Roll20 chat Roomshape: ${result1}    Directions: ${result2}    Cavern-Features: ${result3}    Investigation-Results: ${result4}    }); Can someone help me?
AI cannot create Roll20 macros. It simply does not have enough source material to code properly.  This is not code for Scriptcards. It is an attempt at writing javascript for a brand new script, but AI will not have any way of properly formatting javascript to work inside the Roll20 sandbox.
1708750229

Edited 1708750628
Andrew R.
Pro
Sheet Author
You wasted your time.  You can learn ScriptCard programming by finding a simple problem and using it for that. For example, I use it to automate the abilities of 13th Age monsters, because there’s no NPC functionality in the Roll20 Official 13th Age character sheet. 
1708753293
keithcurtis
Forum Champion
Marketplace Creator
API Scripter
All the others have said is true, but let me clarify a possible misconception. The ban on AI is for Marketplace offerings. You can do whatever you want in your own game. Even if it's a paid game.
Thanks for the replies. What I'm trying to do is run a solo game, where I build some oracles so I don't know what is happening next. I made some rollable tables, and want the macro to pull x number from each table, to generate a room, with exits, possible monsters or npc encounters, and some features to explore, like secret doors, traps, treasure, etc. I don't want it to pull the same result more than once. Can anyone help me with this?
Hey Wesley, I may be able to help you with this next week; this weekend is pretty busy for me. I have some questions for you. For your random generation, you mention you want to pull X number. Do you want X to be random based on number of table entries? Like if you have 8 options for features, do you want to roll 1d8 for number of features? Or would you want to input that number somehow? Also when you say you don't want to generate the same result more than once, do you mean for each room generation or do you mean on subsequent runs? Like if you get a gelatinous cube in room 3, are you ok if room 5 also gets a gelatinous cube or do you want it removed from the available pool of choices after its used in a room?
1708794131
David M.
Pro
API Scripter
In its simplest form, looks like what you are asking can just be performed with a standard macro and rollable tables. You can just use [[1t[tableName]] inline rolls and put them in the template of your choice. e.g.  &default {{Roomshape:[[1t[Roomshape]]]}}  {{ Directions :[[1t[ Directions ]]]}} ...etc. You'd only need a mod script if you wanted additional logic. E.g. if rolls on one table prompted secondary rolls on another. The RecursiveTables script could help with that. For more complicated logic (like Joshua's non-reuse example above), then Scriptcards would likely be your best bet.
Thank you so much. I really appreciate any help. Q: Do you want X to be random based on number of table entries? Like if you have 8 options for features, do you want to roll 1d8 for number of features? A: Random number. One of the tables in Tunnel direction, and the list has 8 options, N, S, E, W, NE, NW, SE, SW. If it's a cave system, I want it to pull 1-8 of those options, but not pull N twice. Q: Also when you say you don't want to generate the same result more than once, do you mean for each room generation or do you mean on subsequent runs? A: This one has things like Random Non-Combat Encounter, Trap, Random Monster, Secret door, and pulls D4 results. As above, I just don't want it to pull Random Monster twice. Joshua N. said: Hey Wesley, I may be able to help you with this next week; this weekend is pretty busy for me. I have some questions for you. For your random generation, you mention you want to pull X number. Do you want X to be random based on number of table entries? Like if you have 8 options for features, do you want to roll 1d8 for number of features? Or would you want to input that number somehow? Also when you say you don't want to generate the same result more than once, do you mean for each room generation or do you mean on subsequent runs? Like if you get a gelatinous cube in room 3, are you ok if room 5 also gets a gelatinous cube or do you want it removed from the available pool of choices after its used in a room?
Thanks for responding. I use Recursive Tables and that is what has allowed me to create the simple macros I have, but in that limit, I don't know how to make it only pull result one time. David M. said: In its simplest form, looks like what you are asking can just be performed with a standard macro and rollable tables. You can just use [[1t[tableName]] inline rolls and put them in the template of your choice. e.g.  &default {{Roomshape:[[1t[Roomshape]]]}}  {{ Directions :[[1t[ Directions ]]]}} ...etc. You'd only need a mod script if you wanted additional logic. E.g. if rolls on one table prompted secondary rolls on another. The RecursiveTables script could help with that. For more complicated logic (like Joshua's non-reuse example above), then Scriptcards would likely be your best bet.