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

Totally new to JavaScript, trying to make a Resolve tracker (just holding values from 0 to 6)

So I'm trying to learn JavaScript, and I'm using a third party system called a Resolve Pool instead of inspiration in my 5e game. I'm trying to write some code to hold the info in a token on the Landing page, then add or subtract from the value as I see fit. I wrote something that doesn't work, trying to modify from a Nick Olivo video based on what I've put together from SkillShare. Needless to say, It doesn't work yet. Right now I just want to get the counting working, then I'll worry about accessing the tokens for data.  on("chat:message", function(msg) {     if (msg.type == "api" && msg.content.indexOf("!resolve") ==0){         var args = msg.content.split(/\s+/);         var isModified = args[1];         var modifierNumber = Number(args[2]);         var resolveDice = 0                  while(arg[1] == true){             resolveDice + modifierNumber;             sendChat("I now have " + resolveDice + " Resolve.")         }         while(resolveDice>=6){             sendChat("Resolve","I feel full of Resolve!")         }         while(resolveDice<=0){             sendChat("Resolve","I have no Resolve.")         }     } }); What I want this code to do is check the chat input for !resolve, followed by a true or false, followed by a number.  When true, I want it to add or subtract the number given and output a message. When false, I want it to output just the number of Resolve Dice. Any Suggestions?
Tried something else, and I seem to also be getting an error, but at least its a new error.  on("chat:message", function(msg) {     if (msg.type == "api" && msg.content.indexOf("!resolve") ==0){         var args = msg.content.split(/\s+/);         var modifierNumber = Number(args[1]);         var resolveDice = 0                  countResolve(modifierNumber)         } }); function countResolve(modifierNumber){     resolveDice + modifierNumber;     if (ressolveDice >= 6){         sendChat("Resolve","I feel full of Resolve!");         ressolveDice = 6;     }     else if (resolveDice <= 0){         sendChat("Resolve","I have no Resolve!");         ressolveDice = 0;     }     else {         sendChat("Resolve","I have " + resolveDice + " Resolve.")     } } I took out the boolean as I realised it wasn't needed. Now I just input a value, modify the result by that value, and I should get a result. 0 should get me the unchanged result. Instead, I get  ReferenceError: ressolveDice is not defined
1662740589

Edited 1662741038
timmaugh
Pro
API Scripter
Hey, Wiggy... welcome to the world of javascript and coding! You're really close. Try this... (analysis after): on('ready', (() => { on("chat:message", function (msg) { if (!(msg.type === "api" && msg.content.indexOf("!resolve") === 0)) return; let args = msg.content.split(/\s+/); if (!args.length) return; let isModified = args[1]; let modifierNumber = Number(args[2] || 0); let resolveDice = 0; if (['yes', 'y', 'true', 'yup', 'keith'].includes(arg[1])) { resolveDice += modifierNumber; sendChat("I now have " + resolveDice + " Resolve."); } if (resolveDice >= 6) { sendChat("Resolve", "I feel full of Resolve!"); } else if (resolveDice <= 0) { sendChat("Resolve", "I have no Resolve."); } }); })); You weren't committing your change to the resolveDice. You were simply adding a number to it, but not assigning it back to resolveDice. Also, you don't want to do a "while" loop if you don't ever make a change that will invalidate the case for sending the message -- that's a good way to get into an infinite loop of messages: message => check case: true => message => check case: true => etc. I changed those to simple If/Else checks so that you get a single message for each of them. Other changes... I inverted your check of the msg.content so that you can simply return and be done with the code indentation if the message doesn't need to be handled by this procedure. Oh, and I put it all inside an on('ready',...) event, so that it will play well with metascripts. (Don't worry about that for now, but later you might like this feature.) There are some helpful links with things like command line parsing, revealing module pattern, etc... I put some together in this post . They might come in handy! EDIT: Oh, and I created a set of things that could pass as 'true' for your first argument: yes, y, true, yup, and keith. Basically, all positive values. Any of them will read as 'true' for the purposes of processing that argument.
1662740758
timmaugh
Pro
API Scripter
_Wiggy said: Tried something else, and I seem to also be getting an error, but at least its a new error.  on("chat:message", function(msg) <...SNIP...>     if (ressolveDice >= 6){ <...SNIP...> Yep, this isn't the name of your variable. Your variable is resolveDice (one 's'). This is where a good linter (a code-verifier) can come in handy. Lots of IDEs have them (Visual Studio, VS Code, etc.), or you can go to a site like jshint.com to have it report potential line errors for you. Then you can track these little beasties down pretty quickly. =D
timmaugh said: Hey, Wiggy... welcome to the world of javascript and coding! You're really close. Try this... (analysis after): on('ready', (() => { on("chat:message", function (msg) { if (!(msg.type === "api" && msg.content.indexOf("!resolve") === 0)) return; let args = msg.content.split(/\s+/); if (!args.length) return; let isModified = args[1]; let modifierNumber = Number(args[2] || 0); let resolveDice = 0; if (['yes', 'y', 'true', 'yup', 'keith'].includes(arg[1])) { resolveDice += modifierNumber; sendChat("I now have " + resolveDice + " Resolve."); } if (resolveDice >= 6) { sendChat("Resolve", "I feel full of Resolve!"); } else if (resolveDice <= 0) { sendChat("Resolve", "I have no Resolve."); } }); })); You weren't committing your change to the resolveDice. You were simply adding a number to it, but not assigning it back to resolveDice. Also, you don't want to do a "while" loop if you don't ever make a change that will invalidate the case for sending the message -- that's a good way to get into an infinite loop of messages: message => check case: true => message => check case: true => etc. I changed those to simple If/Else checks so that you get a single message for each of them. Other changes... I inverted your check of the msg.content so that you can simply return and be done with the code indentation if the message doesn't need to be handled by this procedure. Oh, and I put it all inside an on('ready',...) event, so that it will play well with metascripts. (Don't worry about that for now, but later you might like this feature.) There are some helpful links with things like command line parsing, revealing module pattern, etc... I put some together in this post . They might come in handy! EDIT: Oh, and I created a set of things that could pass as 'true' for your first argument: yes, y, true, yup, and keith. Basically, all positive values. Any of them will read as 'true' for the purposes of processing that argument. For some reason I'm getting  ReferenceError: arg is not defined  when running the code you put. Thank you for your help though! I'm starting to see where some of the errors I origanly made were.
1662744794
timmaugh
Pro
API Scripter
Sorry... Left the 's' off the args variable in the line that checks args[1] against the array. Also, you might want to check both for the length of args AND that the length is > 1... Because it will be at least 1 for having the script handle at [0]. You can either shift() that out of the array (leaving you just the arguments), or test that the length is over 1. If you shift it out of the array, then you are going to have to change all of the indices where you reference the arts object. args[1] would become args[0], etc.
1663752168

Edited 1663762767
Julexar
Pro
API Scripter
_Wiggy said: Tried something else, and I seem to also be getting an error, but at least its a new error.  on("chat:message", function(msg) {     if (msg.type == "api" && msg.content.indexOf("!resolve") ==0){         var args = msg.content.split(/\s+/);         var modifierNumber = Number(args[1]);         var resolveDice = 0                  countResolve(modifierNumber)         } }); function countResolve(modifierNumber){     resolveDice + modifierNumber;     if (ressolveDice >= 6){         sendChat("Resolve","I feel full of Resolve!");         ressolveDice = 6;     }     else if (resolveDice <= 0){         sendChat("Resolve","I have no Resolve!");         ressolveDice = 0;     }     else {         sendChat("Resolve","I have " + resolveDice + " Resolve.")     } } I took out the boolean as I realised it wasn't needed. Now I just input a value, modify the result by that value, and I should get a result. 0 should get me the unchanged result. Instead, I get  ReferenceError: ressolveDice is not defined You might wanna work with Objects and Arrays, maybe even use Arrays to keep track of the resolve of each player. I went ahead and coded something like that. You can find it here You can technically have an infinite amount of players with this one... - but it might slow down with more players Hope this helps ^^
1663775691
The Aaron
Roll20 Production Team
API Scripter
Learning Javascript and Learning the Roll20 Mod Sandbox are related but not the same thing.&nbsp; Here's some links to help: <a href="https://app.roll20.net/forum/post/6605115/namespaces-novice-seeks-help-exploring-the-revealing-module-pattern" rel="nofollow">https://app.roll20.net/forum/post/6605115/namespaces-novice-seeks-help-exploring-the-revealing-module-pattern</a> <a href="https://app.roll20.net/forum/post/6584105/creating-an-object-that-holds-specific-character-dot-id-and-character-name/?pagenum=1" rel="nofollow">https://app.roll20.net/forum/post/6584105/creating-an-object-that-holds-specific-character-dot-id-and-character-name/?pagenum=1</a> <a href="https://app.roll20.net/forum/post/6237754/slug%7D" rel="nofollow">https://app.roll20.net/forum/post/6237754/slug%7D</a> Definitely keep asking questions!