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

How to roll percentile when d20 is rolled and result above 15.

1388205031

Edited 1388205080
DXWarlock
Sheet Author
API Scripter
I've been poking around in API trying to automate some of my campaign things to make the game smoother and more streamline. I cannot for the life of me figure out how to capture dice rolls, read the result, and roll another dice based on it. What i'm trying to do is if a d20 is rolled, and its =<15 then rolls a d100 and returns results (optionally with what that percentage stands for..like 1-10=head, 11-20=Left Arm..etc. This is totally optional and just a nice touch) Anyone know a way to do this? or point me in the right direction to start? Thanks in advance.
1388218690
Sam
Sheet Author
I'm actually working on something that could be converted to what you're looking for with very little effort. I'll put it into a github and post a link here so that you can look at it. Now please bear in mind that while I'm familar with javascript programming, this is my first foray into Roll20 API programming so I don't know if everything will work 100% yet.
1388232850

Edited 1388232896
Stephen S.
Pro
Marketplace Creator
Sheet Author
API Scripter
on("ready", function() { on("chat:message", function(msg) { if(msg.type == "api"){processMessage(msg)}; if(msg.type == "rollresult" || msg.type == "gmrollresult" || msg.inlinerolls !== undefined){ if (msg.inlinerolls !== undefined) { var rollResult = msg.inlinerolls; var rollType = msg.type; }else{ var rollResult = msg.content; var rollType = msg.type; }; processRollResult(rollResult,rollType); }; return; }); }); processRollResult = function(rollResult,rollType){ /Do your thing with the dice results };
1388252432
Sam
Sheet Author
This will fire any time a roll is done?
1388254839
Stephen S.
Pro
Marketplace Creator
Sheet Author
API Scripter
Yes. You might want to throw over msg.who as a variable (var rollBy = msg.who) so you know who threw the dice. Inline results are slightly different to normal rolls (so the type of roll is handy for that) but you should be able to process anything you want.
1388273479

Edited 1388273486
DXWarlock
Sheet Author
API Scripter
Ah perfert Thanks Stephen! That should be plenty of a head start you gave me to get what I actually need to do. What I wanted it to do after it was captured I think I can handle, it was capturing the roll itself, and passing it to something I was fighting with. Much thanks, will start poking around with that bit of code!
1388282607

Edited 1388287123
Stephen S.
Pro
Marketplace Creator
Sheet Author
API Scripter
The idea here is to grab any valid dice rolls in an API message at in point in the message so long as it is: a) valid dice roll and the only thing after the command or b) a dice roll is found when indexing by " " (space.) But it seems less than perfect: if(msgContentMessageLower !== null){ if(msgContentMessageLower.indexOf(" ") == -1){ try{ sendChat("API", "/r " + msgContentMessageLower, function(ops) { if(ops[0].type == "rollresult"){msgDice = ops[0];}; }); }catch(err) { /* do nothing */ }; }else{ var msgParts = msgContentMessageLower.toLowerCase().split(' '); _.each(msgParts, function(indexPart) { if(Object.keys(msgDice).length === 0){ try{ sendChat("API", "/r " + indexPart, function(ops) { if(ops[0].type == "rollresult"){msgDice = ops[0];}; }); }catch(err) { /* do nothing */ }; }; }); }; }; var msgContentMessageLower is whatever followed the API command or null var msgDice = new Object(); is global.... Someone else might need to look closer at it... I sort of view this with suspicion, don't know if its me or Roll20... seems to miss from time to time.