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

[REQUEST]Roll table based on d20 roll

1388783504

Edited 1388783729
DXWarlock
Sheet Author
API Scripter
Not sure if script requests are allowed in the API forum, if not excuse my post. I had another post about doing X when Y roll is made, and got a few replies. Thought I could cobble together what I needed from the examples given in my replies. But its beyond my grasp it seems to work with the API how i need. Would anyone be willing to work out a script that does a if/then on a d20? What I need is simple (to explain, execution of it in API might not be not sure): Basically its all based off d20 rolls. If 15-20 then do 'X' macro also, if 1 then do 'Y' macro also. With the basic dice, there is no way to do an 'if' and 'then' to call a macro. I made macros that reference rollable tables, and shared the macros with everyone. and it works great..just they need to remember to click the macro button when they get the number range. Trying to automate it. I have a critical hit table for extra effects with 15-20 roll, and a fumble table for rolls of 1. So for an example to make it clearer maybe.. Someone rolls a d20, they get a 1. it then calls #fumble macro and runs it as them. Someone rolls a d20, they get a 15-20. it then calls #luck macro and runs it as them. only tricky part is the rolls need to be the actual dice roll result, not based on the roll if they have modifiers added.
1388790749

Edited 1388813930
aRotondi
Sheet Author
So If I understand this correctly, you need it to do the following (which is completely plausible): 1) player rolls dice 2) dice result is within range (15-20, or 1) not including modifiers 3) call the appropriate macro series of events (which references other things maybe) 4) output the result of the dice roll, the roll with modifiers, and the effect of the table in some particular order. that I (and others) can do. I will throw something together once I get home after a few errands, assuming no one snipes me. Feel free to correct my assumptions or provide additional info. (what does the macro look like maybe? table results? formatting concerns?)
1388794039

Edited 1388794183
DXWarlock
Sheet Author
API Scripter
Thats exactly it :) the macros are easy, they are: /me makes a mistake and [[1t[Fumble]]] /me lands a lucky shot on his targets [[1t[Hit-Table]]]. And the tables are just a list of 5-6 things, all with weight of 1. [Below is totally optional on the formatting, if really simple to do, and dont want to push it] Since I'm assuming it will need a different command than /roll for them to use, is it possible to do the roll results in an inline? like they use: /me rolls attack for a [[1d20+3]] So its a lot less chat spam than doing regular rolls and just show the results. Not sure if whatever custom call they will need to do (if any) can be used inside a /me as a [[!whatever]]
1388794177

Edited 1388813664
aRotondi
Sheet Author
Okey Dokey, here's something basic, tell me if this is something like what you would need. a link to a GIST of the product in action! <a href="https://gist.github.com/aRotondi1990/8252112" rel="nofollow">https://gist.github.com/aRotondi1990/8252112</a> note, you can also do some cool things with those sendchat messages like the following: sendChat( who, '&lt;a style="color:DeepSkyBlue"&gt;('+dice+')&lt;/a&gt;'); which changes the output to be ('dice')
1388794266
DXWarlock
Sheet Author
API Scripter
Ah that was quick! Ill test it out :)
1388795809

Edited 1388798682
DXWarlock
Sheet Author
API Scripter
I got it as this, but get error unexpected Identifier on('chat:message',function(msg) { if(msg.type != 'api') return; var logging=true; var parts = msg.content.toLowerCase().split(' '); var who = 'player|'+msg.playerid; if(logging) log( 'message content: '+parts); var command = parts.shift().substring(1); if(command == d20){ var critRange=15; var failRange=1; var mod=0; var roll; var total; _.each(parts,function(curPart){ if(!isNaN(Number(curPart))) mod+= Number(curPart); } roll = randomInteger(20); if(logging) log( 'roll: '+roll); if(roll&gt;=critRange){ sendChat( who, 'Success! ('+roll+')'); sendChat( who, 'total: '+total); sendChat( who, '#Luck'); } else if(roll&lt;=failRange){ sendChat( who, 'Failure! ('+roll+')'); sendChat( who, 'total: '+total); sendChat( who, '#Fumble'); } } }
1388804814

Edited 1388805993
aRotondi
Sheet Author
whoopsie. that's what i get for hardcoding in notepad without checking. the ._each and the end section aren't closed properly; this is the correct section. ._each(parts, function(curPart){ if(!isNaN(Number(curPart))) mod += Number(curPart); }); and at the last line }); as well you probably want to output the roll even if it doesn't fall into one of those two categories, so you can add this to the end: else if(roll&lt;=failRange) { //this section is the same }else { sendChat( who, who+' Rolled ('+total+')'); } EDIT: i apparently suck at ending any code block. corrected. i made a simple macro for the two you put in and it seems to work for me. Let me know if you could how it goes!
1388807440
DXWarlock
Sheet Author
API Scripter
Thanks, sorry was gone to eat dinner, just saw the message. Will give them a try. Thanks for all the hand holding (guess its even more than that, you just handing it to me to start with). :)
1388808020
DXWarlock
Sheet Author
API Scripter
Hmm it took it, no errors. Getting "Spinning up new sandbox..." Seems its loading, but I can throw dice all day..and nothing. get a 1, 15-20..nada :\ Am I being dense and missing something? on("chat:message",function(msg) { if(msg.type != "api") { return; } var logging=true; var parts = msg.content.toLowerCase().split(" "); var who = "player|"+msg.playerid; if(logging) log( "message content: "+parts); var command = parts.shift().substring(1); if(command == d20){ var critRange=15; var failRange=1; var mod=0; var roll; var total; _.each(parts, function(curPart){ if(!isNaN(Number(curPart))) mod += Number(curPart); }); roll = randomInteger(20); if(logging) log( "roll: "+roll); if(roll&gt;=critRange){ sendChat( who, "Success! ("+roll+")"); sendChat( who, "total: "+total); sendChat( who, "#Luck"); } else if(roll&lt;=failRange){ sendChat( who, "Failure! ("+roll+")"); sendChat( who, "total: "+total); sendChat( who, "#Fumble"); } else { sendChat( who, who+' Rolled ('+total+')'); } } });
1388808033

Edited 1388808152
aRotondi
Sheet Author
eh, i'm needing something similar so it was no biggie. Don't know how to parse inline rolls (and thus work with them in general) nearly as well though. Will debug shortly, the 'spinning up sandbox' message usually just means (as far as I understand) it found no errors and is waiting for something to send back to the console.
1388808445
DXWarlock
Sheet Author
API Scripter
It should work for rolled dice correct? like /roll 1d20 or the dice window? And no rush at all, your working for free, I dont expect speedy results here..haha
Ok. I'm an idiot. fixed total not actually being calculated (derp), fixed d20 not being a string (double derp), and made it so that the final case doesn't spit out the player-garble-stream (triple derp). Additionally, now no matter what I do I can't make it take #Luck and actually calculate it. will investigate.
1388809160

Edited 1388809527
aRotondi
Sheet Author
oh man. Quadra mega-derp is in play. Can't directly call macros with the api sendChat function. well we can always directly call the table since that's what your macro is doing anyway. let's see if it does inline as well. nope it fails when i try it. Current workaround is to make 2 calls, one that calls the table directly, and one that puts that value into the flavor text. working on getting that fully operational.
1388810851
DXWarlock
Sheet Author
API Scripter
Sorry for all the hassle, didn't assume it would get this complicated.
1388813187

Edited 1388814312
aRotondi
Sheet Author
William R. said: Sorry for all the hassle, didn't assume it would get this complicated. haha. No it's fine. I'm learning as I go along, and I want to do something similar, so it's good practice. #1 lesson, look at the effin wiki. BUT I HAVE SUCCESS! Which of the following 2 displays would you like to have happen? WOO! so yeah! The parsing statement to get a table result is a bit of a bi-... very frustrating. But as with all challenges, with victory comes the sweet sweet release of dying in battle like a true warrior rolling like you want. NOTE: no weird table shinanery with coloring the result, which I remember was a thing maybe? Here's the code for displaying the 2nd variant (which I think looks cleaner slightly, maybe take out the Success! text). on('chat:message',function(msg) { if(msg.type != 'api') return; var logging=true; var parts = msg.content.toLowerCase().split(' '); var who = 'player|'+msg.playerid; if(logging)log( 'message content: '+parts); var command = parts.shift().substring(1); if(command == 'd20'){ var critRange=15; var failRange=1; var mod=0; var roll; var total=0; var text=''; _.each(parts,function(curPart){ if(!isNaN(Number(curPart))) mod+= Number(curPart); }); roll = randomInteger(20); total = roll+mod; if(logging) log( 'roll: '+roll); if(roll&gt;=critRange){ sendChat('', '/roll 1t[example]',function(results){ text=JSON.parse(results[0].content).rolls[0].results[0].tableItem.name; sendChat(who, '/me lands a lucky shot on his target\'s '+text); sendChat( who, 'Success! ('+roll+')+('+mod+')= '+total); }); }else if(roll&lt;=failRange){ sendChat('', '/roll 1t[example]',function(results){ text=JSON.parse(results[0].content).rolls[0].results[0].tableItem.name; sendChat(who, '/me fails and hits his own '+text); sendChat( who, 'Failure! ('+roll+')+('+mod+')= '+total); }); }else{ sendChat( who,' Roll: ('+total+')'); } } }); EDIT:TYPO CORRECTION IN CODE. will update my first post with the updated code or a link to a Gist (probably better).
1388813963
DXWarlock
Sheet Author
API Scripter
Ah thanks, that looks great! Ive been poking on what you gave me, but I think I was just breaking it more and more, than fixing anything..lol I did manage to somehow lock my campaign up for a few minutes hitting save script after hacking at the first one..so I learned how to do that...if I ever needed a thing like that.
Thanks sir! The first version was written in 10 minutes in notepad with my wife yelling at me to leave for dinner with her, so that might have been a limiting factor on the quality xD. Additionally, if you wanna change the display to the first version in my screenshot, move the 2 sendChat functions outside the inline-call (but still within the if/else section they are contained in). Glad to be of service and if you are working with it right now change the .roll to /roll at the fail section! didn't catch that!
1388814610

Edited 1388815062
DXWarlock
Sheet Author
API Scripter
Yea found that . to / error. I can manage that much without breaking it... How do you syntax it to do bonuses? like !d20+2 or such doesnt seem to work
Ah sorry for that. currently it works via spaces, so you can do '!d20 2' in your chat window and it will work as you describe, without affecting the crits/fails. you can extend this with negative numbers or other things like '!d20 3 -4 12' which will add a total of 11 (3-4+12) to the roll. If you want it to work with +, change the .split(' ') to .split('+'), though that causes issues with using negative numbers and/or spaces. Such examples would then have to be !d20+2 and !d20+3+-4+12 (note the minus sign as well), and spaces will cause bad things :x . Thus by default I leave it with spaces as a separator. I could probably modify it so that it works a bit more like a standard roll20 roll, but it will take a wee bit of time tomorrow Hope that helps!
1388815910
DXWarlock
Sheet Author
API Scripter
That works thanks again for the time and effort! If you feel like making it work with /roll or the die window, would be great! I'm going to poke around in yours, see if I can use it as a baseboard to figure out more stuff to expand it :)
If you would like me to walk you through it, I can do so. The only part that is really weird is the parsing on line 19ish (JSON.parse()...etc), where I had to go get the 'item' from the table and display it as a plain piece of text. also by die window do you mean the 3d dice you can roll in the UI? I've only actually used that once or twice tbh so I don't know how to capture that (yet [i say arrogantly]).
1388867628

Edited 1388867873
DXWarlock
Sheet Author
API Scripter
I think I figured out all what you have done, It works like a champ :) whats weird is adding to it, using like sendchat(who, '/roll 1d6'); doesn't do a 3d dice just the result, but them doing a /roll 1d6 in chat does...not a big deal. maybe an API issue? I also changed var who = 'player|'+msg.playerid; to var who =msg.who otherwise it was a mix of the character name and the players name in the results. But other than that , I tried my damnest to break it rolling dice..and its rock solid what you made. Now for me to figure out how to make it catch inline rolls, AND only inline rolls rolled as to hit :P Most my players are old tabletop players, they like watching the dice roll on the tabletop and waiting to see where it stops :P