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 with status effect script

I'm attempting to make a simple script that adds status tokens to a player's token when they type certain commands into chat. For example, !poison 1 would give the player's token the radioactive symbol with a counter of 1 to represent that they are poisoned for 1 round. I have all of the basic logic working, however I cannot seem to find the token that the player is using because most of the token's values are returning coming back as undefined. All I've been able to do is find the first token in the game's array and apply the status to that token; every time I try to specify which  token I want to manipulate, I get errors because the represents, controlledby, name, etc fields are all undefined no matter what I change in-game. Am I forgetting to do something? Am I just being stupid? Any help is greatly appreciated.
TheAaron's tokenmod script can do this.
That's great, it's clear that the author put a lot of effort into this script, however it does much more than what I'm looking for. Secondarily, I'd much rather learn how to do it myself. I'd also like to know why I'm getting so many undefined variables. I realize you're only trying to simplify things for me and I thank you for that, it's just not the kind of help I'm looking for at the moment. 
1496023396

Edited 1496023504
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
Being able to see your code would help quite a lot in figuring out what is going wrong. I'd guess you aren't using findObjs/getObj correctly or are not using the array/object (respectively) that is returned from it correctly. EDIT: Also, the exact error that you get in the API console would also be helpful.
function RollRight (whoPC) { var character = findObjs({type: 'character',controlledby: whoPC})[0]; return character; } on("chat:message", function(msg) { if(msg.type == "api" && msg.who != "Bot" && msg.content.indexOf("!poison ") !== -1) { var cWho = findObjs({_type: 'character',name: msg.who})[0]; if (!cWho && msg.who.indexOf("(GM)") == -1) { cWho = RollRight(msg.playerid); msg.who = cWho.get("name"); } var poisonRounds = msg.content.replace("!poison ", ""); sendChat("Bot", msg.who + " is poisoned for " + poisonRounds + " rounds!"); var token = findObjs({_type: 'graphic',_subtype: 'token'})[0]; if (!token) { sendChat("Bot", "No valid token!"); } else { //this is where I'd apply the status to the token } } }); The RollRight function is something I found in another thread that simply lets me make sure that the character name is what I'm getting and not the player name. I'm not currently getting any errors since I'm checking to see if the token is valid, but when I try to put any more parameters into the findObjs function for the token, it finds nothing. When I try to get any of the values from the token that it does find when I call it in its current state, they all seem to be undefined.
1496026588

Edited 1496026657
!poison @{target||token_id} Then you can just grab the token using that id instead of the whole character name and everything. msg.content.split(" ")[1] or something similar to grab the token id.
As the GM, you could also use id's from msg.selected to apply the status icons to all the tokens you have selected.
Sky said: !poison @{target||token_id} Then you can just grab the token using that id instead of the whole character name and everything. msg.content.split(" ")[1] or something similar to grab the token id. That should work perfectly for what I'm doing, thank you! I didn't realize you could reference things that way through the chat.
Thought I'd go ahead and update with my current code just to show what you helped me make: function RollRight (whoPC) { var character = findObjs({type: 'character',controlledby: whoPC})[0]; return character; } on("chat:message", function(msg) {     if(msg.type == "api" && msg.who != "Bot" && msg.content.indexOf("!poison ") !== -1)     {         var str = msg.content.replace("!poison ", "");         var tokenid = str.split(" ")[0];         var poisonRounds = str.split(" ")[1];         var token = findObjs({_type: 'graphic',_subtype: 'token',_id:tokenid})[0];         if (token)         {             token.set("status_drink-me", poisonRounds);             var cWho = findObjs({_type: 'character',name: msg.who})[0];         if (!cWho && msg.who.indexOf("(GM)") == -1)        {         cWho = RollRight(msg.playerid);     msg.who = cWho.get("name");       }             sendChat("Bot", msg.who + " is poisoned for " + poisonRounds + " rounds!");         }     } }); on("change:campaign:turnorder", function(obj) {     if(!obj.get("turnorder")) return;     var turnorder = JSON.parse(obj.get("turnorder"));     if (!turnorder.length) return;     if (turnorder[turnorder.length - 1].id == -1) return;     var token = getObj("graphic", turnorder[turnorder.length - 1].id);     if (token.get("status_drink-me") > 1)     {         token.set("status_drink-me", token.get("status_drink-me") - 1);     }     else token.set("status_drink-me", false); }); I now have it tracking the turn order so that it can reduce the counter each round and then get rid of the marker once the counter is done. Next I'm going to clean things up a bit and then implement the rest of the status effects. Thanks again for the assistance.
You might double check what happens if you use that command on a drawing and not a token.
1496069184

Edited 1496077382
If it's a token that I've marked as a drawing, it works just fine. If it's something drawn with one of the drawing tools such as draw shape or freehand, nothing happens. I assume that since regular drawings have no tokenid, it simply ignores them. EDIT: Here's the most recent version with all of the 5e conditions plus bless and haste: function RollRight (whoPC) {     var character = findObjs({type: 'character',controlledby: whoPC})[0];     return character; } class Condition {     constructor(cmd, chatForm, token)     {         this.cmd = cmd;         this.chatForm = chatForm;         this.token = token;     } } const Bless = new Condition("!bless ", "blessed", "status_angel-outfit"); const Blind = new Condition("!blind ", "blinded", "status_pummeled"); const Charm = new Condition("!charm ", "charmed", "status_broken-heart"); const Deaf = new Condition("!deaf ", "deafened", "status_interdiction"); const Fright = new Condition("!fright ", "frightened", "status_screaming"); const Grapple = new Condition("!grapple ", "grappled", "status_fishing-net"); const Haste = new Condition("!haste ", "hasted", "status_fluffy-wing"); const Incap = new Condition("!incap ", "incapacitated", "status_arrowed"); const Invis = new Condition("!invis ", "invisible", "status_ninja-mask"); const Paral = new Condition("!paral ", "paralyzed", "status_aura"); const Petrif = new Condition("!petrif ", "petrified", "status_frozen-orb"); const Poison = new Condition("!poison ", "poisoned", "status_drink-me"); const Prone = new Condition("!prone ", "prone", "status_back-pain"); const Restrain = new Condition("!restrain ", "restrained", "status_cobweb"); const Stun = new Condition("!stun ", "stunned", "status_lightning-helix"); const Uncon = new Condition("!uncon ", "unconscious", "status_sleepy"); on("chat:message", function(msg) {     if(msg.type == "api" && msg.who != "Bot")     {         var cond;         if (msg.content.indexOf(Bless.cmd) !== -1)             cond = Bless;         else if (msg.content.indexOf(Blind.cmd) !== -1)             cond = Blind;         else if (msg.content.indexOf(Charm.cmd) !== -1)             cond = Charm;         else if (msg.content.indexOf(Deaf.cmd) !== -1)             cond = Deaf;         else if (msg.content.indexOf(Fright.cmd) !== -1)             cond = Fright;         else if (msg.content.indexOf(Grapple.cmd) !== -1)             cond = Grapple;         else if (msg.content.indexOf(Haste.cmd) !== -1)             cond = Haste;         else if (msg.content.indexOf(Incap.cmd) !== -1)             cond = Incap;         else if (msg.content.indexOf(Invis.cmd) !== -1)             cond = Invis;         else if (msg.content.indexOf(Paral.cmd) !== -1)             cond = Paral;         else if (msg.content.indexOf(Petrif.cmd) !== -1)             cond = Petrif;         else if (msg.content.indexOf(Poison.cmd) !== -1)             cond = Poison;         else if (msg.content.indexOf(Prone.cmd) !== -1)             cond = Prone;         else if (msg.content.indexOf(Restrain.cmd) !== -1)             cond = Restrain;         else if (msg.content.indexOf(Stun.cmd) !== -1)             cond = Stun;         else if (msg.content.indexOf(Uncon.cmd) !== -1)             cond = Uncon;         if (cond)         {             var str = msg.content.replace(cond.cmd, "");             var tokenid = str.split(" ")[0];             var rounds = str.split(" ")[1];             var token = findObjs({_type: 'graphic',_subtype: 'token',_id:tokenid})[0];             if (token)             {                 var cWho = findObjs({_type: 'character',name: msg.who})[0];                 if (!cWho && msg.who.indexOf("(GM)") == -1)                 {                     cWho = RollRight(msg.playerid);                     msg.who = cWho.get("name");                 }                 if (rounds != "indef")                 {                     token.set(cond.token, rounds);                     sendChat("Bot", msg.who + " is " + cond.chatForm + " for " + rounds + " rounds!");                 }                 else                 {                     token.set(cond.token);                     sendChat("Bot", msg.who + " is " + cond.chatForm + "!");                 }             }         }     } }); on("change:campaign:turnorder", function(obj) {     if(!obj.get("turnorder")) return;     var turnorder = JSON.parse(obj.get("turnorder"));     if (!turnorder.length) return;     if (turnorder[turnorder.length - 1].id == -1) return;     var token = getObj("graphic", turnorder[turnorder.length - 1].id);     if (token.get(Bless.token) > 1)         token.set(Bless.token, token.get(Bless.token) - 1);     else if (token.get(Bless.token) == 1)         token.set(Bless.token, false);     if (token.get(Blind.token) > 1)         token.set(Blind.token, token.get(Blind.token) - 1);     else if (token.get(Blind.token) == 1)         token.set(Blind.token, false);     if (token.get(Charm.token) > 1)         token.set(Charm.token, token.get(Charm.token) - 1);     else if (token.get(Charm.token) == 1)         token.set(Charm.token, false);     if (token.get(Deaf.token) > 1)         token.set(Deaf.token, token.get(Deaf.token) - 1);     else if (token.get(Deaf.token) == 1)         token.set(Deaf.token, false);     if (token.get(Fright.token) > 1)         token.set(Fright.token, token.get(Fright.token) - 1);     else if (token.get(Fright.token) == 1)         token.set(Fright.token, false);     if (token.get(Grapple.token) > 1)         token.set(Grapple.token, token.get(Grapple.token) - 1);     else if (token.get(Grapple.token) == 1)         token.set(Grapple.token, false);     if (token.get(Haste.token) > 1)         token.set(Haste.token, token.get(Haste.token) - 1);     else if (token.get(Haste.token) == 1)         token.set(Haste.token, false);     if (token.get(Incap.token) > 1)         token.set(Incap.token, token.get(Incap.token) - 1);     else if (token.get(Incap.token) == 1)         token.set(Incap.token, false);     if (token.get(Invis.token) > 1)         token.set(Invis.token, token.get(Invis.token) - 1);     else if (token.get(Invis.token) == 1)         token.set(Invis.token, false);     if (token.get(Paral.token) > 1)         token.set(Paral.token, token.get(Paral.token) - 1);     else if (token.get(Paral.token) == 1)         token.set(Paral.token, false);     if (token.get(Petrif.token) > 1)         token.set(Petrif.token, token.get(Petrif.token) - 1);     else if (token.get(Petrif.token) == 1)         token.set(Petrif.token, false);     if (token.get(Poison.token) > 1)         token.set(Poison.token, token.get(Poison.token) - 1);     else if (token.get(Poison.token) == 1)         token.set(Poison.token, false);     if (token.get(Prone.token) > 1)         token.set(Prone.token, token.get(Prone.token) - 1);     else if (token.get(Prone.token) == 1)         token.set(Prone.token, false);     if (token.get(Restrain.token) > 1)         token.set(Restrain.token, token.get(Restrain.token) - 1);     else if (token.get(Restrain.token) == 1)         token.set(Restrain.token, false);     if (token.get(Stun.token) > 1)         token.set(Stun.token, token.get(Stun.token) - 1);     else if (token.get(Stun.token) == 1)         token.set(Stun.token, false);     if (token.get(Uncon.token) > 1)         token.set(Uncon.token, token.get(Uncon.token) - 1);     else if (token.get(Uncon.token) == 1)         token.set(Uncon.token, false); });
Looks good... I went another way with my own icon setting script. !icon status-name|# token-id