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

Script Worker question

1611005476

Edited 1611031272
Hello all, So, I'm struggling to get something basic working. I'm using a custom system. The jist of the system is that different attribute values role different dice whish should average the attribute value. Example small example: 7=3d3+1 8=3d4+1 9=4d3+1 10=4d4 So, as the attribute changes. the roll changes. I'm trying ta have a button         <div>             <input type="hidden" name="attr_sd" vaule="1d3-1"/>             <input type="number" name="attr_strength" min="1" value="9" max="29"/>             <button type="roll" name="roll_strengthcheck" value="/r @{sd}"></button>         </div> I have a the following script which should look up the new dice string for the roll value and store it in sd. const parseValues = (values, stat, type='int') => {          if(type === 'int') return parseInt(values[stat])||0;          else if(type === 'float') return parseFloat(values[stat])||0;          else if(type === 'str') return values[stat];          else return 0;     };     const attributeDiceToRoll =(value) => {     console.log("attribute value "+value);     switch(value) {         case 7:             return "3d3+1";             break;         case 8:             return "3d4+1";             break;         case 9:             return "4d3+1";             break;         case 10:             return "4d4";             break;     } }; on("change:strength sheet:opened", function() {     let strength     getAttrs( [         "strength"     ], function(values) {             strength = parseValues(values, "strength");             console.log("Strength "+strength);             let sdice = attributeDiceToRoll(strength);             console.log("sdice "+sdice);             setAttrs({                 "sd":sdice              }); //setAttrs         }); // getAttrs }); // onChange ---------- Clicking the roll_strengthcheck button shows in the chat:  There was an error with your formula. Please try again.  While, the console is showing a  SyntaxErr expected: Array(13), found: null, message: "Expected "(", ".", "[", "abs(", "ceil(", "d", "flo…, [ |\t], [+|\-] or [0-9] but end of input found.", offset: 0, …} Any tricks to figuring out where this is coming from would be appreciated.  Thank you for reading.
1611007044
The Aaron
Roll20 Production Team
API Scripter
Should that be: /r @{ selected| sd}
Hello Aarnon, Thank you. Is that required if they are doing it from the character sheet? Does the player have to select their token to be able to use actions on their character sheet? It seems like there should be a better way, but maybe there isn't. Thank you
1611016421
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
Nope, you don't need selected there. When you use a roll button from a sheet, all the attributes refer to the character that the button was clicked on by default. You only need to specify a character (or a selected token) if you're doing something like calling another button (and there's ways around that with sheetworkers to customize the secondary roll call) or calling an attribute from another character. I think what is happening is that your sheetworker is erroring out and may be corrupting the value of your sd attribute. I'm heading into a game, but I'll read through your script after that.
Thank you Scott. That's what I thought, but I wasn't positive. 
1611028732
GiGs
Pro
Sheet Author
API Scripter
Your sheet worker refers to a parseValues function. Is this a mistake, or a function declared elsewhere in your script? Should that be parseInt? eg.  strength = parseInt(values.strength) || 7;  This line needs some error checking:  let sdice = attributeDiceToRoll(strength); It's going to cause an error if strength is ever a value other than 7, 8, 9, or 10, and you need to handle that.
I added, via an edit, the parseValues function (which was an example I picked up here someplace).  Yeah, I just was providing an example of the switch statement. I didn't include all the possible values here for brevity. From a troubleshooting standpoint, I thought it would be enough to test the changing values for dice roles and get the root issue resolved. I hope at least. :-)
1611039015

Edited 1611039111
GiGs
Pro
Sheet Author
API Scripter
hehe, I wrote that parseValues function. These days I prefer separate functions for int and float, like const int = (score, error = 0) => parseInt(score) || error; const float = (score, error = 0) => parseFloat(score) || error; And then this line  strength = parseValues(values, "strength"); would be  strength = int(values.strength); These versions allow you to pass different default values, when you need something other than 0, like let result = int(values.stat, 1); The second optional parameter is what the variable is set to, if there's an error in the parseInt. By the way, why is your let strength line before the getAttrs? You dont need that line, just declare the variable where it is needed, like let strength = parseValues(values, "strength"); I just noticed your message has an error report: SyntaxErr expected: Array(13) , found: null, message: "Expected "(", ".", "[", "abs(", "ceil(", "d", "flo…, [ |\t], [+|\-] or [0-9] but end of input found.", offset: 0, …} I'm not great at reading roll20 error messages, but that makes me think the error is in this line (or at least want to examine in more detail):  let sdice = attributeDiceToRoll(strength); Is it possible you are passing values that are outside the range that you check for in the switch? Seeing the complete attributeDiceToRoll function would be helpful - does the switch have a default:  option? I wouldnt use a switch there, I'd use a javascript object as table of data like: const attributeDiceToRoll =(value) => {     console.log("attribute value "+value);     const diceTable = {         7: "3d3+1",         8: "3d4+1",         9: "4d3+1",         10: "4d4"     };     return diceTable.hasOwnProperty(value) ? diceTable[value] : "1d3-1"; }; This function has some very rudimentary error checking. the diceTable.hasOwnProperty(value) part checks to make sure that value actually exists as an entry in the table. If it doesnt, it returns a default damage dice of 1d3-1. You'd call this version of the function the same way: let sdice = attributeDiceToRoll(strength);
Thank you GiGs. I'll implement your suggestions tonight and report back. I appreciate the feedback.
Thank you all for the responses. I have things working now! Your input is much appreciated. 
1611114793
GiGs
Pro
Sheet Author
API Scripter
great!