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

In script dice roll

I have looked through various scripts and forums but didn't see it anywhere so thought I would just ask. When your in the API and writing a script is it possible to create a variable that holds the results of a dice role?

Code Flow:

If X >= Y then process A else process B. I need a way (if possible) to generate a roll into a variable to determine if is above or below a target number.
June 16 (11 years ago)
Lithl
Pro
Sheet Author
API Scripter

You have several options.

  • If the user used the /roll command (or an alias), the msg.type will be "rollresult" and the msg.content will be a JSON string describing the roll they made.
  • If the user included one or more inline rolls in their message, msg.inlinerolls will be defined, and contain an array of roll data.
  • You can generate the same randomness as a die roll with the randomInteger function. For example, if you need to "roll" 1d20 from the script, you can use var dieValue = randomInteger(20);
  • You can send a complete roll to the dice engine with sendChat, and capture the result by supplying a third parameter to the function. For example:
sendChat("Riley", "/roll 1d20+4", function(ops) {
    // ops will be an ARRAY of command results.
    var rollresult = ops[0];
    //Now do something with rollresult, just like you would during a chat:message event...
});
June 17 (11 years ago)

Edited June 17 (11 years ago)
Great, thanks Brian. I appreciate the info.

I just tried the sendchat code and got the following error:

For reference, the error message generated was: ReferenceError: rollresult is not defined at Sandbox.<anonymous> (evalmachine.<anonymous>:95:279) at eval (

Ron P. said:

Great, thanks Brian. I appreciate the info.

I just tried the sendchat code and got the following error:

For reference, the error message generated was: ReferenceError: rollresult is not defined at Sandbox.<anonymous> (evalmachine.<anonymous>:95:279) at eval (

Can you post the code?
I think I got around the error, but now it is giving "undefined" as the result when running:

sendChat("Riley", "/roll 1d20+4", function(ops) {
// ops will be an ARRAY of command results.
var rollresult = ops[0];
//Now do something with rollresult, just like you would during a chat:message event...

});

I just did some testing. If I define the variable outside of the sendchat i get undefined. If I leave the variable as it is then I get the refrence error.
June 17 (11 years ago)
The Aaron
Roll20 Production Team
API Scripter
sendChat() returns immediately and execution continues. When you write something like:
log('Before Send Chat');
sendChat("Riley", "/roll 1d20+4", function(ops) { 
    log("Inside Send Chat");
});
log("After Send Chat");

The output is:
"Before Send Chat"
"After Send Chat"
"Inside Send Chat"

You need to do all your processing inside the sendChat() function, or better still, call back from it into your other code.