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

Currently Flabergasted

I wrote a script to change a token's HP (bar3_value) either by healing or doing damage and it doesn't seem to be working on PCs. It works PERFECT on NPCs I've had no issues at all there but when I use it on a PCs token nothing happens. Here is the script: //By Kastion&nbsp; //<a href="https://app.roll20.net/users/3173313/kastion" rel="nofollow">https://app.roll20.net/users/3173313/kastion</a> //version: 1.0 //use !change-hp &lt;damage|healing&gt; @{selected|character_id} to update character HP. on('ready', function() { &nbsp; &nbsp; on('chat:message', function(msg) { &nbsp; &nbsp; &nbsp; &nbsp;if (msg.type == "api" && msg.content.indexOf("!change-hp") !== -1) { &nbsp; &nbsp; &nbsp; &nbsp;let args = msg.content.split(/\s+/); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;var c = getObj('character', args[3].trim()); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;var t = findObjs({ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;_type: 'graphic',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;represents: args[3].trim() &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;})[0];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if(t && !isNaN(args[2])) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var bar_value = parseInt(t.get("bar3_value")); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var hp_dif = parseInt(args[2]) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var old_hp = bar_value; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (args[1] == "damage") &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var new_hp = bar_value - hp_dif; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else if (args[1] == "healing")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var new_hp = bar_value + hp_dif; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; log("Type of HP change not specified!"); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; t.set({ bar3_value: new_hp }); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (new_hp &lt; old_hp) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sendChat('', "/desc " + c.get("name") + " takes " + hp_dif + " points of damage."); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sendChat('', "/desc " + c.get("name") + " heals " + hp_dif + " points of damage."); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; &nbsp; &nbsp;}; &nbsp; &nbsp; }); }); And the script is called using this macro: !change-hp ?{Change:|damage|healing} ?{Total:|0} @{selected|character_id} I have no idea &nbsp; why this would work on NPCs but none of the player characters. Can someone help me out here?
1526554231

Edited 1526554251
MyRoll20Stuffs
API Scripter
I'm thinking this has something to do with there being multiple tokens that are representing the character and not all of them are updating when one is updated by this function..
I figured it out. By looping over findObjs instead of getting just the 0 position in the array I'm updating every single token and represents the character by looping through the findObjs list.&nbsp; Some times you just need to talk it out (or write it out I suppose) to get your brain working to figure something out.
Here's the updated script in case anyone wants to use it: //By Kastion&nbsp; //<a href="https://app.roll20.net/users/3173313/kastion" rel="nofollow">https://app.roll20.net/users/3173313/kastion</a> //version: 1.0 //use !change-hp &lt;damage|healing&gt; @{selected|character_id} to update character HP. on('ready', function() { &nbsp; &nbsp; log("-=&gt; Change HP Tool Loaded &lt;-= [Last Edited by Kastion May 17th 2018]") &nbsp; &nbsp; on('chat:message', function(msg) { &nbsp; &nbsp; &nbsp; &nbsp;if (msg.type == "api" && msg.content.indexOf("!change-hp") !== -1) { &nbsp; &nbsp; &nbsp; &nbsp;let args = msg.content.split(/\s+/); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;var c = getObj('character', args[3].trim()); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;var t = findObjs({ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;_type: 'graphic',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;represents: args[3].trim() &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;});&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var i = 0, changed = 0; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for (i = 0; i &lt; t.length; i++)&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if(t[i] && !isNaN(args[2]))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var bar_value = parseInt(t[i].get("bar3_value")); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var hp_dif = parseInt(args[2]) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var old_hp = bar_value; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (args[1] == "damage") &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var new_hp = bar_value - hp_dif; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else if (args[1] == "healing")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var new_hp = bar_value + hp_dif; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; log("Type of HP change not specified!"); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; t[i].set({ bar3_value: new_hp }); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; changed = 1; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (changed) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (new_hp &lt; old_hp) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sendChat('', "/desc " + c.get("name") + " takes " + hp_dif + " points of damage."); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sendChat('', "/desc " + c.get("name") + " heals " + hp_dif + " points of damage."); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; &nbsp; &nbsp;}; &nbsp; &nbsp; }); });
1526595184
Naughty Zoot
Marketplace Creator
good job
Hi Kast, can you explain in more detail what this script does? Instead of the player adding/subtracting the #, you are controlling the hp?
1526629156
GiGs
Pro
Sheet Author
API Scripter
It allows you to create a macro that anyone can use (ideally, set it up as a token action), like the one suggested: !change-hp ?{Change:|damage|healing} ?{Total:|0} @{selected|character_id} And it assumes whatever you use for HP is linked to a token's bar3 Whenever you have a token selected, and click the macro button, you'll be prompted for damage or healing, then prompted for a number, and it will add or subtract that to the number in bar3. That will in turn alter the HP on the character sheet, for those tokens that are linked that way. Kastion: you might want to look into msg.selected. You could get rid of the final element of the macro and with a loop, set up the macro so that the GM could apply damage or healing to multiple characters in one action.
Kastion: you might want to look into msg.selected. You could get rid of the final element of the macro and with a loop, set up the macro so that the GM could apply damage or healing to multiple characters in one action. I'll do some reading and see what I can come up with. It would certainly help with AoE heals/damage.
1526638395
GiGs
Pro
Sheet Author
API Scripter
A quick look at your script and from memory (I'm about to rush out), the key line is: var c = getObj('character', args[3].trim()); For single selections, cChange that to&nbsp; var c = msg.selected[0]; For groups: var selected = msg.selected; then just loop through selected, the same way you do the looping through t later, with the code you have after that inside the loop. for(let s = 0; s &lt; msg.selected.length; s++) { var c = msg.selected[s]; etc. (The above is typing quickly from memory, but you're already using all the skills you need to do this, so you can figure it out.)
Thanks for the quick mockup of what I need to update. Appreciate it G G!
1526651501
The Aaron
Pro
API Scripter
Note that msg.selected contains {_type: 'something', _id: 'id string'} objects, so you'll need to turn those into tokens: let chars = msg.selected.map( (o) =&gt; getObj('graphic',o._id) ).filter( (t) =&gt; undefined !== t); I generally ignore the _type and specify my own then filter on undefined.&nbsp; It's an easy way to get rid of any drawings or text you accidentally select.&nbsp; Then you can either take the first entry, or loop over them.&nbsp; Idiomatic Javascript these days is to use the Array.forEach(): chars.forEach( (c) =&gt; { // use c for something.. });
Completely off topic but a good example for me to say this.&nbsp; This is a very odd forum to me because of the enormous spread in the level of expertise between those who create content for the community to use, and the community who uses it.&nbsp; Sometimes threads are full of great advice and cool stuff that I can use, and other times the threads might as well be written in Sanskrit.&nbsp; It's unusual to me to see users and creators sharing the same forum. Don't get me wrong, I like it.&nbsp; It just sometimes makes me feel pretty dumb.&nbsp; :P
1526656817
Jakob
Sheet Author
API Scripter
Idiomatic Javascript these days is to use the Array.forEach(): aha! I knew you'd come around some day.
1526657114

Edited 1526657137
The Aaron
Pro
API Scripter
Jakob said: aha! I knew you'd come around some day. HA!&nbsp; I wrote that EXPLICITLY for you Jakob.&nbsp; =D&nbsp; Old habits are hard to break (does that mean they don't make habits like they used to? =D ).&nbsp; Must be an Eastern Hemisphere thing... Kryx is always harassing me about underscore. =D Walrus said: Completely off topic but a good example for me to say this.&nbsp; This is a very odd forum to me because of the enormous spread in the level of expertise between those who create content for the community to use, and the community who uses it.&nbsp; Sometimes threads are full of great advice and cool stuff that I can use, and other times the threads might as well be written in Sanskrit.&nbsp; It's unusual to me to see users and creators sharing the same forum. Don't get me wrong, I like it.&nbsp; It just sometimes makes me feel pretty dumb.&nbsp; :P Pretty cool, right?&nbsp; &nbsp;We all have to start somewhere and you're never to old to learn more (Thanks Jakob!).&nbsp; If there's ever anything you'd like to know, I'm happy to explain it in excruciating detail.&nbsp; It might always be Sanskrit to you, but everyone is wired differently and I don't mind helping if I can. =D
One the helping topic, I see that this is built for bar 3 health, but I might be mistaken&nbsp; but isn't the default on most tokens, especially drag and drop, that it's bar 1. Why would you make it for bar 3, I'm guessing you are not playing DnD? Also is this the only place I would have to change for this to work for bar 1?&nbsp; t[i].set({ bar3_value: new_hp }); t[i].set({ bar1 _value: new_hp });
1526659646

Edited 1526659681
The Aaron
Pro
API Scripter
I use bar3 for DnD as well.&nbsp; You can change the default bars in the Game Settings, and on the character sheet settings, generally.&nbsp; In my case, I'm using shaped, which will handle it automatically in the script. Bar3 is nice for HP (in my opinion) because it's the leftmost bubble, and defaults to red.&nbsp; I use Bar1 (green) for AC, and Bar2 (blue) for speed or other.&nbsp; I realize you can change the colors in the Settings Tab, but I also like where the bar ends up on the token (mostly) which you can't change.&nbsp; (I do change my status markers to be along the bottom.) Edit: That is the right place to change if you want to use bar1 instead.
Yeah I use Bar 1 for health on Shaped sheet, blue for AC, and the Red for Temp HP. Because there isn't a real good way of handling temp HP without the sheet open.
1526664851
keithcurtis
Forum Champion
Marketplace Creator
API Scripter
Walrus said: Completely off topic but a good example for me to say this.&nbsp; This is a very odd forum to me because of the enormous spread in the level of expertise between those who create content for the community to use, and the community who uses it.&nbsp; Sometimes threads are full of great advice and cool stuff that I can use, and other times the threads might as well be written in Sanskrit.&nbsp; It's unusual to me to see users and creators sharing the same forum. Don't get me wrong, I like it.&nbsp; It just sometimes makes me feel pretty dumb.&nbsp; :P I feel this occasionally. We have an odd confluence of technologies and skill sets. Programmers, Web developers, Artists, Writers, Dabblers in one but tyros in another. I'm pretty comfortable with most stuff but the Javascript. Fortunately, it's a pretty level-headed and helpful community.
1526670344
Jakob
Sheet Author
API Scripter
The Aaron said: Jakob said: aha! I knew you'd come around some day. HA!&nbsp; I wrote that EXPLICITLY for you Jakob.&nbsp; =D&nbsp; Old habits are hard to break (does that mean they don't make habits like they used to? =D ).&nbsp; Must be an Eastern Hemisphere thing... Kryx is always harassing me about underscore. =D That's what I suspected :D.