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

Script that would launch when a character was hit?

Basically, I'm wondering if there was a script that could launch another script or macro when a character received damage from an attack. I'm trying to figure out a way to give one of my players the option to cast their shield spell as a reaction. Thanks in advance.
1620410150
The Aaron
Roll20 Production Team
API Scripter
You can certainly write an API script that reacts to an attribute or token bar changing. Is that sufficient, or do you need to have it happen before damage is applied?  That would be harder to manage. 
No, I don't need it to happen before damage is applied. I have no problem fixing the result after. I just want to make sure that my player remembers they have the option of casting shield as a reaction, so the script activating after the character is hit is fine.
1620411433
The Aaron
Roll20 Production Team
API Scripter
Are you only changing the bars directly, or do you use scripts to change the hp value?
Just the bars.
1620412132
The Aaron
Roll20 Production Team
API Scripter
Here's a super basic script that will do that.  Replace the bolded character id with the character id of the character that you want to notify.  you can specify multiple characters by separating the quoted character ids with a , in that [ ]: on('ready',()=>{ const cids=[' -MOgNGXam0kerQCuSUrf ']; const attrnames = ['hp']; //////////////////////////////////////////////////////////// const handleHPChange = (obj,prev)=>{ if(cids.includes(prev._characterid) && attrnames.includes(obj.get('name').toLowerCase())){ if(parseFloat(obj.get('current'))<parseFloat(prev.current)){ let c = getObj('character',prev._characterid); if(c){ sendChat('',`/w "${c.get('name')}" You appear to have taken damage, should you use Shield?`); } } } }; on('change:attribute',handleHPChange); });
Thanks Aaron, I'll check it out when I get home.
1620415784

Edited 1620415975
Quick question, Aaron. Am I not separating the character Ids properly because it fires for the first character, but doesn't fire for the second one? This is what I have: on('ready',()=>{ const cids=['-MSdHhHvsg7NvrW0lDS7','-MS_WOvdwoOAw29bjwri']; const attrnames = ['hp']; //////////////////////////////////////////////////////////// const handleHPChange = (obj,prev)=>{ if(cids.includes(prev._characterid) && attrnames.includes(obj.get('name').toLowerCase())){ if(parseFloat(obj.get('current'))<parseFloat(prev.current)){ let c = getObj('character',prev._characterid); if(c){ sendChat('',`/w "${c.get('name')}" You appear to have taken damage, should you use Shield?`); } } } }; on('change:attribute',handleHPChange); });
1620416579
The Aaron
Roll20 Production Team
API Scripter
That looks right.  You could verify by swapping their places, but if you'd made a mistake in the format, it wouldn't run the script. It will first check if the character ID is in that array, then check if the attribute name is in the second array ('hp').  Next it will check if the value decreased, and finally, it will load the character and send the message. I'd suggest verifying that you have the right character ids, and that the attribute is named "hp" and the value is actually being decreased.
Okay Aaron, I think I solved it. At first, I could only get one character to work and I couldn't get the script to work with any other characters, so I decided to look at each individual character token and see what was different about them. Then I noticed that the token that worked was not only under the player's control, but also under GM control. Once I added GM control to the other tokens, it worked as intended. Thanks for helping me out on this, I appreciate it.
I do have one question though. Is it possible to launch a macro from the script?