I recently started looking at creating my own script and happened upon this thread, Community Forums: I Wrote My First API Script: It Allows the GM to Whisper the Player's Controlling the Selected Tokens | Roll20: Online virtual tabletop . This is a cool script but I found an issue with it and I'm not sure how to fix it. The code that I have has been modified slightly, not much really but is here, on("ready", function()
{
on("chat:message", function(msg)
{
if (msg.type=="api" && msg.content.indexOf("!whisp ")==0)
{
let PlayerList = new Set();
(msg.selected||[]).forEach((obj) => {
let token = getObj('graphic', obj._id);
if (token) {
let character = getObj('character', token.get('represents'));
if (character) {
const controlled = character.get('controlledby').split(",");
controlled.forEach(c => PlayerList.add(c));
}
}
});
const TheMessage = msg.content.slice(7);
if (PlayerList.has('all')) {
sendChat('the voice in your head', TheMessage);
} else {
(PlayerList||[]).forEach((player) => {
let PlayerVar = getObj("player", player) ;
let PlayerName = PlayerVar.get("_displayname");
sendChat('the voice in your head', `/w "${PlayerName}" ${TheMessage}`);
});
}
};
});
}); The problem begins if I don't have a player token selected and send the whisper. I get an undefined error as follows. For reference, the error message generated was: TypeError: Cannot read properties of undefined (reading 'get')
TypeError: Cannot read properties of undefined (reading 'get')
at apiscript.js:16110:47
at Set.forEach (<anonymous>)
at apiscript.js:16108:34
at eval (eval at <anonymous> (/home/node/d20-api-server/api.js:169:1), <anonymous>:65:16)
at Object.publish (eval at <anonymous> (/home/node/d20-api-server/api.js:169:1), <anonymous>:70:8)
at /home/node/d20-api-server/api.js:1763:12
at /home/node/d20-api-server/node_modules/firebase/lib/firebase-node.js:93:560
at hc (/home/node/d20-api-server/node_modules/firebase/lib/firebase-node.js:39:147)
at Kd (/home/node/d20-api-server/node_modules/firebase/lib/firebase-node.js:93:546)
at Id.Mb (/home/node/d20-api-server/node_modules/firebase/lib/firebase-node.js:93:489) I need some help fixing this so that my sandbox doesn't crash in this event. Not sure where to go for help. Also, is there a way that I can set this up for my players to use? They can't choose other players tokens so this doesn't work for them. Thanks in advance.