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

Help understanding some basics

1732625773

Edited 1732625883
Hiya, real quick simple question here. I'm playing around with the scripting right now in preparation of writing some stuff for my game. I have very little experience with JS so forgive me if I'm just missing something obvious, but I can't for the life of me see what I'm doing wrong here. Right now all I'm trying to do is get the script to give me some info in the chat about a token I have selected, just to familiarize myself with how to access it. I have the following script running on a test game. When I select a token the script seems to work up until I ask it for the token's name. This is the result I get in chat when I have a token named "Test Shopkeeper" selected. Why is the name of the token undefined? I had the same issue when trying to read the "represents" id value in order to access the character this token is tied to. What am I missing here? Something obvious I'm sure but I cant figure out what the problem is. 
1732632988

Edited 1732647473
keithcurtis
Forum Champion
Marketplace Creator
API Scripter
Hi Chris! Try element.get("name") object.attribute only works for certain attributes, IIRC. I thought it only worked for _id, but apparently it also works for _type.
And just a bit of advice: whenever posting any code for help, it's always best practice to post the code itself so that others can copy and paste it, instead of just a screenshot of the code. This is true for macros and other chat commands as well. Screenshots are more helpful for showing output and error messages. :) Good luck starting scripting!
Jarren said: And just a bit of advice: whenever posting any code for help, it's always best practice to post the code itself so that others can copy and paste it, instead of just a screenshot of the code. This is true for macros and other chat commands as well. Screenshots are more helpful for showing output and error messages. :) Good luck starting scripting! Yeahh I usually would but I figured as simple as this particular piece of code was that wouldn't be necessary. I'll do it here though on("chat:message",function(msg){     if(msg.type=="api" && msg.content.indexOf("!tokenInfo")==0){                  var selectedArray = msg.selected;         if (selectedArray == undefined) {sendChat("token test", "Please select a token"); return;}                      sendChat("token test",selectedArray.length.toString() + " tokens selected");         selectedArray.forEach((element) =>          sendChat("token element " + selectedArray.indexOf(element),"I am a " + element._type));         selectedArray.forEach((element) =>          sendChat("char check " + selectedArray.indexOf(element),"My name is " + element.name));     } });         
keithcurtis said: Hi Chris! Try element.get("name") object.attribute only works for certain attributes, IIRC. I thought it only worked for _id, but apparently it also works for _type. Thanks for the answer, I actually did try that first before posting and it throws an error Which reallly confused me lol. I mean element._type is telling me that what I'm passing into the forEach() is in fact an array of objects and that it has one member which is a "graphic", so I have no idea why it won't let me .get off of it. I actually wrote this little troubleshooting script to figure that out because I've been trying to do a script that uses object.get() and I've been getting this error message. I was originally trying to get the "represents" id so I could get to the token's character sheet and received this error, so I've been trying to see what I can and can't get from an object and getting inconsistent results. I thought maybe it was something with how I was implementing forEach, which I'm going to need to use at some point since I'm ultimately trying to interact with a repeating section on the character sheet. I also tried the shorthand _.forEach() just to see and had the same results.
Okay, I figured it out. It seems like msg.selected doesn't give you the object, just its _id and _type. So what I needed to do was this selectedArray.forEach(function(element) {         const obj = getObj(element._type,element._id);         sendChat("char check " + selectedArray.indexOf(element),"My name is " + obj.get("name"));         }); I see now that msg.selected is its own kind of object that just has _id and _type, so it makes sense it won't let me .get() haha. Appreciate the responses still though, I guess I just needed to step away and come back to it.