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

Running a Script in a Macro Under Certain Conditions

1536502223
Elemental Flame
Pro
Marketplace Creator
I have a wild magic sorcerer and I am attempting to set up the surges. I know my options were a rollable table, which I have almost no experience with, or a script. I added a script that allows a player to type !wildmagic and it'll roll and display what the result is in a very clean way. I am hoping to create a macro that this player can click whenever they cast a spell that will roll a 1d20. If they roll a 1, I want it to automatically run the !wildmagic script. I can get a macro to roll a 1d20. I can get it to run !wildmagic. I can't get it to only do it on certain results of that 1d20 roll though. Any tips, advice, or flat out telling me what to do would be greatly appreciated!
1536504026
The Aaron
Pro
API Scripter
This would probably be easiest to do with the API.  In fact, it would be easiest to combine the !wildmagic command with a check against a d20.  What script are you using? It should be a simple modification...
1536506152
Elemental Flame
Pro
Marketplace Creator
I googled and found it so I'm not sure that I can attribute the author since I only found the github link and don't know who that is on here, but the link to the API I am using is:&nbsp; <a href="https://github.com/jfflbnntt/roll20-api-scripts/tree/master/WildMagicSurgeTables" rel="nofollow">https://github.com/jfflbnntt/roll20-api-scripts/tree/master/WildMagicSurgeTables</a>
1536508712
The Aaron
Pro
API Scripter
Ok. Here's what I propose,&nbsp; I'll add a second command which checks the result of a d20 first to determine if there was a surge: !wildmagic-check and is otherwise identical to the regular command. Script: // By: Jefe // Contact: <a href="https://app.roll20.net/users/451842/jefe" rel="nofollow">https://app.roll20.net/users/451842/jefe</a> var WildMagicSurgeTable = WildMagicSurgeTable || (function() { 'use strict'; var version = 0.5, rangeMax = 100, apiCommand = "!wildmagic", helpMsg = "Usage - !wildmagic [--help|-h] [--private|-w [whisperTo]], rolls on the wildmagic table, optionally whispers result to roller if --private is used or &lt;whisperTo&gt; if present i.e. (-w gm will send the results to the GM as a whisper). '--help' will return this message.", tableName = "Wild Magic Table", msgTemplate = "&amp;{template:default} {{name=Wild Magic Surge}} {{roll=!roll}} {{result=!result}}", table = [ {range: [1,2], result: "Roll on this table at the start of each of your turns for the next minute, ignoring this result on subsequent rolls."}, {range: [3,4], result: "For the next minute, you can see any invisible creature to which you have line of sight."}, {range: [5,6], result: "A MODRON chosen and controlled by the DM appears in an unoccupied space within 5ft of you, then disappears 1 minute later."}, {range: [7,8], result: "You cast FIREBALL as a 3rd level spell, centered on yourself."}, {range: [9,10], result: "You cast MAGIC MISSLE as a 5th level spell"}, {range: [11,12], result: "Your height changes [[d10]] inches. You shrink if the number is Odd, grow if the number is even."}, {range: [13,14], result: "You cast CONFUSION centered on yourself." }, {range: [15,16], result: "For the next minute you gain 5hp at the start of each of your turns." }, {range: [17,18], result: "You grow a long beard made of feathers that remains on your face until you sneeze, at which point the beard explodes out from your face." }, {range: [19,20], result: "You cast GREASE centered on yourself." }, {range: [21,22], result: "Creatures have disadvantage on saving throws against the next spell you cast that requires a saving throw in the next minute." }, {range: [23,24], result: "Your skin turns a vibrant shade of Blue. A REMOVE CURSE spell can end this effect." }, {range: [25,26], result: "An eye appears in the center of your forehead for the next minute. You gain advantage on Perception checks that involve sight." }, {range: [27,28], result: "For the next minute, all your spells with a casting time of 1 action now have a casting time of 1 bonus action." }, {range: [29,30], result: "You teleport up to 60ft to an unoccupied space of your choice that you can see." }, {range: [31,32], result: "You are transported to the Astral Plane until the end of your next turn, after which time you return to the space you previously occupied, or the nearest space that isn't." }, {range: [33,34], result: "Maximize the damage of the next damaging spell you cast within the next minute." }, {range: [35,36], result: "Your age changes [[d10]] years. You age if the number is Odd, become younger if the number is even."}, {range: [37,38], result: "[[1d6]] FLUMPHS controlled by the DM appear in unoccupied spaces within 60ft of you and are frightened of you. They vanish after 1 minute."}, {range: [39,40], result: "You regain [[2d10]] hit points."}, {range: [41,42], result: "You turn into a potted plant until the start of your next turn. While a plant, you are incapacitated and have vulnerability to all damage. If you drop to 0 hit points, your pot breaks and you revert to your form."}, {range: [43,44], result: "For the next minute, you can teleport up to 20ft as a bonus action on each of your turns."}, {range: [45,46], result: "You cast LEVITATE on yourself."}, {range: [47,48], result: "A UNICORN controlled by the DM appears in an unoccupied space within 5ft of you, then disappears 1 minute later."}, {range: [49,50], result: "You can't speak for the next minute. Whenever you try, pink bubbles float out of your mouth."}, {range: [51,52], result: "A spectral shield hovers near you for the next minute, granting you a +2 bonus to AC and immunity to MAGIC MISSLE."}, {range: [53,54], result: "You are immune to being intoxicated by alcohol for the next [[5d6]] days."}, {range: [55,56], result: "Your hair falls out, but grows back within the next 24hrs."}, {range: [57,58], result: "For the next minute, any flammable object you touch that isn't being worn or carried by another creature bursts into flame."}, {range: [59,60], result: "You regain your lowest-level expended spell slot."}, {range: [61,62], result: "For the next minute, you must shout when you speak."}, {range: [63,64], result: "You cast FOG CLOUD centered on yourself."}, {range: [65,66], result: "Up to three creatures you choose within 30ft of you take [[4d10]] lightning damage."}, {range: [67,68], result: "You are frightened of the nearest creature until the end of your next turn."}, {range: [69,70], result: "Each creature within 30ft of you becomes invisible for the next minute. The invisibility ends on a creature when it attacks or casts a spell."}, {range: [71,72], result: "You gain resistance to all damage for the next minute."}, {range: [73,74], result: "A random creature within 60ft of you becomes poisoned for [[1d4]] hours."}, {range: [75,76], result: "You glow with bright light in a 30-foot radius for the next minute. Any creature that ends its turn within 5ft of you is blinded until the end of its next turn."}, {range: [77,78], result: "You cast POLYMORPH on yourself. If you fail the saving throw, you turn into a sheep for the spell's duration."}, {range: [79,80], result: "Illusory butterflies and flower petals flutter in the air within 10ft of you for the next minute."}, {range: [81,82], result: "You can take one additional action immediately."}, {range: [83,84], result: "Each creature within 30ft of you takes [[1d10]] Necrotic damage. You regain hit points equal to the sum of the damage dealt."}, {range: [85,86], result: "You cast MIRROR IMAGE."}, {range: [87,88], result: "You cast FLY on a random creature within 60ft of you."}, {range: [89,90], result: "You become invisible for the next minute. During that time, other creatures cannot hear you. The invisibility ends if you attack or cast a spell."}, {range: [91,92], result: "If you die within the next minute, you immediately come back to life as if by a REINCARNATE spell."}, {range: [93,94], result: "Your size increases by 1 category for the next minute."}, {range: [95,96], result: "You and all creatures within 30ft of you gain vulnerability to piercing damage for the next minute."}, {range: [97,98], result: "You are surrounded by faint, ethereal music for the next minute."}, {range: [99,100], result: "You regain all expended Sorcery Points!"}, ], writeResult = function(msg, rollResult, isPrivate, whisperTo) { var message = msgTemplate.replace('!roll', rollResult.roll).replace('!result', rollResult.result) var speakingAs = msg.who || tableName; if(isPrivate){ if (!!whisperTo) { sendChat(speakingAs, "/w "+whisperTo+" "+message); } message = "/w "+msg.who+" "+message; speakingAs = tableName; } sendChat(speakingAs, message); }, rollOnTable = function() { var roll = randomInteger(rangeMax); var checkRange = function(entry){ return entry.range.indexOf(roll) !== -1 }; var tableEntry = _.find(table, checkRange); return { roll: roll, result: tableEntry.result }; }, handleInput = function(msg) { var args, option, isPrivate = false, whisperTo = null; if(msg.type !== "api") { return; } args = msg.content.split(/\s+/); let checkNum = 0; switch(args[0]){ case `${apiCommand}-check`: checkNum = randomInteger(20); case apiCommand: if(args.length &gt; 1) option = args[1] if (option == "--help" || option == "-h") { sendChat(tableName, helpMsg); return; } if(option == "--private" || option == "-w") { isPrivate = true; if (args.length &gt; 2) { whisperTo = args[2]; } } if(checkNum) { if(checkNum !== 1) { sendChat('',`${ isPrivate ? `/w "${ null!==whisperTo ? whisperTo : msg.who}" `: ''}&lt;div style="font-size: .6em; padding: .1em 1em; font-weight:bold; border: 1px solid #999; border-radius: .25em; background-color: #ccc;"&gt;Wild Magic Check: [[${checkNum}]] -- &lt;span style="color: #006600;"&gt;No Surge&lt;/span&gt;&lt;/div&gt;`); break; } } writeResult(msg, rollOnTable(), isPrivate, whisperTo); break; } }, checkInstall = function() { log(tableName+' v'+version+' Ready'); }, registerEventHandlers = function() { on("chat:message", handleInput); }; return { CheckInstall: checkInstall, RegisterEventHandlers: registerEventHandlers }; }()); on('ready', function() { 'use strict'; WildMagicSurgeTable.CheckInstall(); WildMagicSurgeTable.RegisterEventHandlers(); });
1536508815
The Aaron
Pro
API Scripter
Output like this.&nbsp; If it doesn't roll a 1, it just outputs what it rolled.&nbsp; If it does roll a 1, it outputs like !wildmagic does: Wild Magic Check:&nbsp; 4 &nbsp;--&nbsp; No Surge 11:56AM Dragonpriest: Wild Magic Surge Roll 41 Result You turn into a potted plant until the start of your next turn. While a plant, you are incapacitated and have vulnerability to all damage. If you drop to 0 hit points, your pot breaks and you revert to your form. 11:56AM : Wild Magic Check:&nbsp; 13 &nbsp;--&nbsp; No Surge Wild Magic Check:&nbsp; 9 &nbsp;--&nbsp; No Surge Wild Magic Check:&nbsp; 18 &nbsp;--&nbsp; No Surge
1536508852
The Aaron
Pro
API Scripter
(apologies for the funky formatting... screenshot uploading wasn't working)
1536510032
Elemental Flame
Pro
Marketplace Creator
Amazing work! Thank you for taking the time! With that working, I am going to try fiddling with the text output to see if I can tailor it a bit to the sorcerer in my game so it looks neat from a roleplay perspective. I really appreciate your help.
1536514082
Elemental Flame
Pro
Marketplace Creator
Aaron, I am hoping you're having a boring Sunday and don't mind if I pick your brain. It was rather easy to discern the message you added when there is no surge and I was able to customize that for my sorcerer. I'm not quite understanding how it displays the entry from the table when there IS a surge. I was hoping to add an emote or a message on the line just before it puts up the table entry to say that the sorcerer experiences a wild magic surge or something more flavorful.&nbsp; The only other times I see the sendChat command are here: The Aaron said: Script: writeResult = function(msg, rollResult, isPrivate, whisperTo) { var message = msgTemplate.replace('!roll', rollResult.roll).replace('!result', rollResult.result) var speakingAs = msg.who || tableName; if(isPrivate){ if (!!whisperTo) { sendChat(speakingAs, "/w "+whisperTo+" "+message); } message = "/w "+msg.who+" "+message; speakingAs = tableName; } sendChat(speakingAs, message); }, case apiCommand: if(args.length &gt; 1) option = args[1] if (option == "--help" || option == "-h") { sendChat(tableName, helpMsg); return; I'm not seeing how that displays the table entry and I'm not entirely certain how to add my own line before it, if that's possible. Would I need to enter my message and then a return or break command to put the entry on the next line/message? If it isn't clear, I am utterly inept with scripting, heh.
1536518024
The Aaron
Pro
API Scripter
No day is ever boring with 8 kids, but pick away! =D In that quoted script segment above, the message is being constructed on the second line.&nbsp; There's a template string on line 11 of the script that looks like this: msgTemplate = "&amp;{template:default} {{name=Wild Magic Surge}} {{roll= !roll }} {{result= !result }}", The bolded and underlined parts are being replaced by the results of the rollOnTable() function (line 78), which just does the lookup into the table array on line 12.&nbsp; The template uses the default Roll Template, which gives the tabular formatting and purple color. The if(isPrivate){ ... } block then prepends a whisper if that was requested. There are 2 places you could consider customizing this output.&nbsp; If you just want to prepend some text to the existing formatting, you could do that prepending before the if(isPrivate){ block, like this: var message = msgTemplate.replace('!roll', rollResult.roll).replace('!result', rollResult.result) var speakingAs = msg.who || tableName; message = `&lt;div&gt; some stuff here&lt;/div&gt; ${message}`; if(isPrivate){ or you could change the actual template if you want to abandon the default Roll Template: msgTemplate = "&lt;div&gt;&lt;h2&gt;Wild Surge!!&lt;/h2&gt;&lt;p&gt;Dear Wild Mage,&lt;br&gt;&lt;br&gt;Your roll of [[!roll]] has earned you a result on this test of: !result&lt;br&gt;&lt;br&gt;Please be advised that future performance of this nature may affect your grade at the midterm.&lt;br&gt;&lt;br&gt;Kind regards,&lt;br&gt;&lt;br&gt;&lt;i&gt;--The infinite&lt;/i&gt;&lt;/p&gt;&lt;/div&gt;"; Hope that helps!
1536537747
Elemental Flame
Pro
Marketplace Creator
I was not able to get the message command to work, but you telling me about that line allowed me to add a sendChat command that will go off before the wild magic table is rolled so it works great! I have no idea how you even have time to read the forums with eight kids, so consider me EXTRA appreciative of your help, lol.