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

Marvel Heroic and Cortex Prime Script made

1700054477

Edited 1700061958
chat prompt command: !mhrroll Copy / Paste text below in Game Settings. Go to Next Post for 3D dice fix. on("chat:message", function(msg) {     if(msg.type == "api" && msg.content.indexOf("!mhrroll ") !== -1) {         var diceString = msg.content.replace("!mhrroll ", "");         var diceArray = diceString.split(" ");         var rolls = [];         var rollText = "";         // Roll each dice in the input         diceArray.forEach(function(dice) {             var parts = dice.split("d");             var numberOfDice = parseInt(parts[0]) || 1;             var diceSize = parseInt(parts[1]);             for (var i = 0; i < numberOfDice; i++) {                 var result = randomInteger(diceSize);                 rolls.push(result);                 rollText += "d" + diceSize + ": " + result + " ";             }         });         // Sort and sum the top two rolls         rolls.sort(function(a, b) { return b - a });         var total = rolls[0] + (rolls.length > 1 ? rolls[1] : 0);         // Output the result         sendChat(msg.who, "/em rolls " + rollText + " | Total: " + total);     } }); function randomInteger(max) {     return Math.floor(Math.random() * max) + 1; }
3D dice Roller incorporated. My bad. Fixed . Because the prompt needs to be /roll and not !mhrroll. Example of NEW API; /roll 2d8 + 2d6. on ( "chat:message" , function ( msg ) { if (msg. type == "api" && msg. content . indexOf ( "!mhrroll " ) !== - 1 ) { var diceString = msg. content . replace ( "!mhrroll " , "" ); var diceArray = diceString. split ( " " ); var rollExpressions = []; // Build individual roll expressions for each dice diceArray. forEach ( function ( dice ) { var parts = dice. split ( "d" ); var numberOfDice = parseInt (parts[ 0 ]) || 1 ; var diceSize = parseInt (parts[ 1 ]); for ( var i = 0 ; i < numberOfDice; i++) { rollExpressions. push ( "1d" + diceSize); } }); // Join the roll expressions with '+' and roll the highest two dice var finalRollExpression = rollExpressions. join ( " + " ) + "kh2" ; // Send the roll expression to Roll20 sendChat (msg. who , "/roll " + finalRollExpression); } });
1700154399
The Aaron
Roll20 Production Team
API Scripter
Hi Robert S.  I made a few minor adjustments to your script, if you're interested.  I put it inside the on('ready',...) event, which will prevent it from responding to messages before the sandbox is ready.  This also makes it easier to work with MetaScripts, which rely on the order of registration of the chat:message  event. I changed the way the command is matched to only match on the start of the line.  This will permit you to pass commands as the arguments to other scripts, such as MetaScripts. I put in a different method of splitting the arguments which does the same thing, but is a little cleaner (handles multiple spaces and strips off the command in one go). Finally, I changed from var to let for all the variables.  The semantics for let are less surprising than the ones for var, and modern code is suggested to use let and const instead of var (there's only one place I use var now, but that's another story. =D) on('ready', ()=>{ on("chat:message", function(msg) { if(msg.type == "api" && /^!mhrroll(\b\s|$)/i.test(msg) ) { let diceArray = msg.content.split(/\s+/).slice(1); let rollExpressions = []; // Build individual roll expressions for each dice diceArray.forEach(function(dice) { let parts = dice.split("d"); let numberOfDice = parseInt(parts[0]) || 1; let diceSize = parseInt(parts[1]); for (let i = 0; i < numberOfDice; i++) { rollExpressions.push("1d" + diceSize); } }); // Join the roll expressions with '+' and roll the highest two dice let finalRollExpression = rollExpressions.join(" + ") + "kh2"; // Send the roll expression to Roll20 sendChat(msg.who, "/roll " + finalRollExpression); } }); });
Hey thanks Aaron! Cool because initially I went "ITS WORKING!" and then went back later. Wait, now its just tallying up all the dice again vs Keeping the Highest 2 (kh2).  Will give this a shot. Thanks!