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 .
×
A third party outage is causing loading issues for a select number of games. Reach out to our Help Center if you're affected!
Create a free account

Calculate bonus from checkbox

1551666878

Edited 1551667185
Eddas
Sheet Author
my html, javascript, and css code is all here:&nbsp; <a href="https://jsfiddle.net/wizardeddas/md2ve4ar/2/" rel="nofollow">https://jsfiddle.net/wizardeddas/md2ve4ar/2/</a> I'm working on doing some auto calculation stuff. Right now the ability score modifiers calculate properly. Now I'm working on the saving throws. The three saving throws are based on constitution, dexterity, and mind. there is a checkbox next to each saving throw to determine if the character has a proficiency in that save. So it should check to see if the checkbox is checked, if it is it should set the saving throw to the ability modifier plus proficiency, and if its not checked, it should just be set to the ability modifier. But for some reason no matter what I do, its not setting anything. Any thoughts? Oh and I did open developer tools in chrome to see if it was throwing errors. I caught a misspelling of one attribute, and it kept complaining about variables not being defined, so that's why I have all those var statements at the top of the JS.
1551668608
GiGs
Pro
Sheet Author
API Scripter
Minor tip: I'd suggest setting your inputs that are supposed to be numbers to type="number", not type="text". It stops players accidentally entering bad data. The problem is your getAttrs line: you aren't including the proficiencies there. When you use the line let fortchecked = parseInt(values.fortitude_proficiency)||0; it fails because fortitude_proficiency is not in the getAttrs array, so its value cant be found. The second problem is this bit Math.floor(values.dexterity / 2)-5+proficiency_bonus; proficiency_bonus isnt declared or set anywhere. You should add that to the change: and getAttrs lines too. By the way, this if statement could be improved if (fortchecked === 1) { let fortbonus = Math.floor(values.constitution / 2)-5+proficiency_bonus; } else { let fortbonus = Math.floor(values.constitution / 2)-5; } I'd use const fortbonus = Math.floor(values.constitution / 2)-5 + (fortchecked === 1 ? proficiency_bonus : 0); This is just another way to write this: let fortbonus = Math.floor(values.constitution / 2)-5; if(fortchecked === 1) fortbuns += proficiency_bonus;
1551670207
Eddas
Sheet Author
Hi GiGs, Thank you for the quick reply. I've instituted your changes and it's partially working. It sets the saving throws based on the ability scores, which is great. But it's still not adding the proficiency modifier when I check the checkboxes. It also tells me that proficiency_bonus is not defined, but I did add it to the getattrs&nbsp;array, so I'm not sure why that would be? Here is my updated code: <a href="https://jsfiddle.net/wizardeddas/md2ve4ar/3/" rel="nofollow">https://jsfiddle.net/wizardeddas/md2ve4ar/3/</a> Thank you for all your help, I really appreciate&nbsp;it!
1551670882
GiGs
Pro
Sheet Author
API Scripter
It's because you haven't declared it like so: const proficiency_bonus = parseInt(values.proficiency_bonus)||0; You need it in the getAttrs array,&nbsp; and &nbsp;need to declare it within the code with var, let, or const. (A simple guide: use let &nbsp;for variables that will change after you declare them, use const &nbsp;for variables that are fixed and don't change. All the variables you declare in this worker could be const .) Also if proficiency_bonus is a stat that ever changes, you'll need to add a change:proficiency_bonus &nbsp;too. Note: i trust you realise you also need to add reflex_proficiency and mind_proficiency to the getAttrs line too?
1551671571

Edited 1551675293
GiGs
Pro
Sheet Author
API Scripter
One way to explain the getAttrs and variable bit: The array in the getAttrs line simply creates a pool of attributes that is available for use in your script. They are stored in an object called values (or whatever you put in the function(values) bit. So this code: getAttrs(["strength", "dexterity", "constitution"], function(values) { Creates a a javascript object (a type of variable that contains multiple attributes), which looks like this: values = {strength: 12, dexterity: 13, constitution 8}; The numbers there are grabbed from the character sheet. A sheet worker has no access at all to the rest of a character sheet: it only has access to attributes you declare in the getAttrs function. So that means every attribute you want to use in your sheet worker must be included in the getAttrs array. And then to actually use the attributes, you must extract them from that object. One way is to create a variable, like const str = values.strength; Notice the values. part? that's essentially saying "look in the data object named values, find the strength attribute, and grab its score". You can also use the attribute directly without assigning to a variable, as you have with the stat scores: strength_modifier: Math.floor(values.strength / 2)-5, That's why values. is there in that expression. I hope this clears up some confusion :)
1551671857
Eddas
Sheet Author
Thank you so much.
1551675229
GiGs
Pro
Sheet Author
API Scripter
You're welcome :)