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

Issue getting selected tokens from msg

February 09 (3 years ago)

   ok, I am sure this is going to be a SMH moment, but I cannot get the selected token resolved to the character to gather information for a new initiative script I am writing for the API compatible SW character sheet.  I have scavenged some old code of mine I wrote for my C&C game, but I am missing something.  I have a token selected and am getting an error "For reference, the error message generated was: TypeError: Cannot read property 'id' of undefined

TypeError: Cannot read property 'id' of undefined at apiscript.js:18:49"


var StarWarsFFGScripts = {
	name: 'Star Wars FFG API Compatible Scripts',
	version: '0.0.1',
	author: 'Scott Westberg',
	lastUpdate: '02/08/2021'	
} 
log('-=> '+StarWarsFFGScripts.name+' v'+StarWarsFFGScripts.version+' <=- '+StarWarsFFGScripts.author+' '+StarWarsFFGScripts.lastUpdate)

on('ready', function() {
  on("chat:message", function(msg) {

    if (msg.type === 'api' && msg.content == "!rollInitCool") {
        
      var selectedToken = msg.selected
      var tokenID = selectedToken._id
      var tokenObj = getObj("graphic", tokenID)
      var characterObj = getObj('character',(tokenObj||{get:()=>{}}).get('represents'));
      var initCool = getAttrByName(characterObj.id,'npc-minionskillVigilance','current')
      
      sendChat('Initiative',initCool)

    }
  })
})


February 09 (3 years ago)
timmaugh
Pro
API Scripter

Hey, Scott...

msg.selected is an array.

You need to extract the token you want... [0] for the first, but there could be more (if you select more). If you have more than one, you can do a forEach() over the array.

February 09 (3 years ago)

Edited February 09 (3 years ago)

Thanks, I swear I had tried that, but maybe not...  I normally do a group of selected tokens to roll initiative as the GM so that is normally in place with a forEach() calling a function to perform the rolls...  Like I said above, SMH!!!


It is working now.