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

Is there a API script for producing math results on miss

I was wondering if there was an API that would produce the results of the miss whispered to the DM.  This would allow the DM to see why you missed.  So If you rolled less than a 10 you flat out missed.  If you rolled between 11 and the AC mod the blow bounced off the armor.  Etc.  I dont want it put in general chat, just whispered to me and possibly color coated.  Make it easier to add flavor description to combat.  I used to do this at the table, but I was already doing all the math in my head to determine what happened.  VTT make it easy to just say hit or miss and move on.
1728840220

Edited 1728840268
I dont write API scripts, I generated the below with chatgpt: on("chat:message", function(msg) { if (msg.type === "api" && msg.content.indexOf("!attack") === 0) { // Check if a token is selected if (!msg.selected || msg.selected.length === 0) { sendChat("System", "/w gm Please select a target token."); return; } // Parse the roll let args = msg.content.split(" "); if (args.length < 2) { sendChat("System", "/w gm Usage: !attack [roll]"); return; } let rollExpression = args[1]; // Get the targeted token's ID let targetToken = getObj("graphic", msg.selected[0]._id); let characterID = targetToken.get("represents"); // If no character is linked to the token if (!characterID) { sendChat("System", "/w gm The selected token does not represent a character."); return; } // Retrieve the character's AC components let baseAC = getAttrByName(characterID, "baseAC") || 10; // Default base AC if not specified let dexMod = getAttrByName(characterID, "dexMod") || 0; let armorBonus = getAttrByName(characterID, "armorBonus") || 0; let shieldBonus = getAttrByName(characterID, "shieldBonus") || 0; // Calculate the total AC let totalAC = parseInt(baseAC) + parseInt(dexMod) + parseInt(armorBonus) + parseInt(shieldBonus); // Roll the dice for the attack roll sendChat("player|" + msg.playerid, "/r " + rollExpression, function(ops) { let rollResult = JSON.parse(ops[0].content); let attackRoll = rollResult.total; let resultMsg = ""; // Determine if the roll hits or misses and at which layer if (attackRoll < baseAC) { resultMsg = `Miss with Base AC (${baseAC})`; } else if (attackRoll < (baseAC + dexMod)) { resultMsg = `Miss with Dex (${baseAC + dexMod})`; } else if (attackRoll < (baseAC + dexMod + armorBonus)) { resultMsg = `Miss with Armor (${baseAC + dexMod + armorBonus})`; } else if (attackRoll < totalAC) { resultMsg = `Miss with Shield (${totalAC})`; } else { resultMsg = "Hit"; } // Whisper the result to the GM sendChat("System", `/w gm ${resultMsg}`); }); } });
1728882263

Edited 1729435400
timmaugh
Pro
API Scripter
The metascripts can do that. The easiest way to see it is to start with a command line, so let's just posit one... Given a to hit roll of 1d20 And a damage roll of 2d8 ...where a selected character was attacking a targeted token and... ...the value the to-hit roll needed to beat was the AC attribute on the targeted token's character sheet... ...then... ...an imaginary starting command line for an attack might be: &{template:default} {{name=@{selected|token_name} Attacks!}} {{To Hit=[[1d20]] }} {{Tarrget AC=@{target|AC} }} {{Damage=[[2d8]] }} To use this and expand it into what you want, we're going to need another command. The above command is the attack, which hits the chat output. In addition to this, we need to potentially send a message to the GM if the attack misses. We'll need to use the to-hit roll to determine whether to send the message, which means we're going to need it in 2 messages. We can do that in a ZeroFrame batch set of commands: !{{(^) {^&template:default} ({)name=@{selected|token_name} Attacks!(}) ({)To Hit=[[1d20]] (}) ({)Tarrget AC=@{target|AC} (}) ({)Damage=[[2d8]]  (}) {&if $[[0]] > @{target|AC} }   /w gm {^&template:default} ({)name=Near Miss!(}) ({)Missed By=[\[\ @{target|AC} - $[[0]] \]\] (}) ({)Result=get.TableMule.NearMiss.$[[3]].value/get (}) {&end} }} The above assumes you've setup a character named "TableMule" and on that character you've created an ability named "NearMiss". That ability should be structured with the range of your miss values and the result you want to report: <=10=Miss 11-12=Bounces off armor 13-15=Localized gravity event pulls shot off-course 16-18=An animatronic carp stops the shot 19=A grazing shot >20=Projectile lodged in armor I can explain the various parts of what's going on, there, and I will if you decide to try this version out. EDIT: Corrected ":" that was in the first template command, above, replacing it with an "=" as discussed below.
1729426683

Edited 1729428033
Hey Timmaugh thanks for the reply. I tested this in my test macro game.  I create a  "TableMule"  and create a new ability  "NearMiss" and pasted into the current box the table with the miss values.  It does not seem to work.  I got a chat response when I posted the first example attack line you provided, but the expanded example doesnt result in a whisper or any results that I can see in chat window.  It does ask for a target. How would this work (once we get it working) on say a group attack like burning hands?
1729431478
timmaugh
Pro
API Scripter
Did you install the Metascript Toolbox? Reading through my previous message, I apparently forgot to explicitly include that part, and that could explain the results you got. I can convert the Burning Hands attack, but you'll have to share it, first. I don't play DnD except to support the sheet with scripts, so I don't have a fluency with the rules in that way.
1729432321

Edited 1729432430
burning hands hits multiple targets, they get a dex save for half damage or full damage if they fail.  If you write it up with a save and how to apply the attack to multiple targets I can cannabilize it for D&D (I think). EDIT: nevermind i found metascript
Metascript fixed the no results, however the macro now displays: Iojassoet Fresheau Attacks! To Hit: "inlinerollresult showtip tipsy-n-right" style="-webkit-tap-highlight-color: rgba(0,0,0,0);font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif;color: #404040;line-height: 1.25em;box-sizing: content-box; background-color: #FEF68E; padding: 0 3px 0 3px; font-weight: bold; cursor: help; font-size: 1.1em;border: 2px solid #FEF68E;" title="Rolling 1d20 = (&lt;span class="basicdiceroll"&gt;7&lt;/span&gt;)"&gt;7 Tarrget AC 12 Damage <span class="userscript-inlinerollresult userscript-showtip userscript-tipsy-n-right" title="Rolling 2d8 = ( 7 + 7 )" style="box-sizing: content-box; font-family: &quot;helvetica neue&quot;, helvetica, arial, sans-serif; color: rgb(64, 64, 64); line-height: 1.25em; background-color: rgb(254, 246, 142); padding: 0px 3px; font-weight: bold; cursor: help; font-size: 1.1em; border: 2px solid rgb(254, 246, 142);">14
1729435350
timmaugh
Pro
API Scripter
Whoops!! That's my fault. I left a ":" where a "=" should have been. Look right after the "To Hit" header in the template layout... replace that colon with the equal sign. I'll fix it above, and repost it here, just for clarity: !{{(^) {^&amp;template:default} ({)name=@{selected|token_name} Attacks!(}) ({)To Hit=[[1d20]] (})&nbsp;({)Tarrget AC=@{target|AC} (})&nbsp;({)Damage=[[2d8]]&nbsp; (}) {&amp;if $[[0]] &gt; @{target|AC} } &nbsp; /w gm {^&amp;template:default} ({)name=Near Miss!(}) ({)Missed By=[\[\ @{target|AC} - $[[0]] \]\] (}) ({)Result=get.TableMule.NearMiss.$[[3]].value/get (}) {&amp;end} }} Sorry! That's what I get for posting too quickly! Let me know as you cannibalize this for your purposes if you run into issues and I'll try to help.
ok my D&amp;D game has started I will test it out later.&nbsp; thanks for the help BTW