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 plays a sound upon a bars value being lowered and vice versa.

Does anyone know of a Script that does this? One someone's made or a feature of a MOD?
1681050849

Edited 1681050906
keithcurtis
Forum Champion
Marketplace Creator
API Scripter
Hi Kadin! I wrote this last week: Does anyone know of a script that allows a sound (or random playlist of sounds) to play when a tokens health bar is reduced at all? I could modify it to play on a bar being raised as well, if you could use that.
Oh wow! Thanks and yes I'd have a lot of use for a script that plays it upon being raised if it isn't any trouble!
1681059701

Edited 1681066123
keithcurtis
Forum Champion
Marketplace Creator
API Scripter
Try this: on('ready', () => {     let playSound = function (trackname, action) {         let track = findObjs({ type: 'jukeboxtrack', title: trackname })[0];         if (track) {             track.set('playing', false);             track.set('softstop', false);             if (action == 'play') {                 track.set('playing', true);             }         }         else {             sendChat('Damage Sound', '/w gm No Track Found...');             log("No track found " + trackname);         }     }     on("change:graphic:bar1_value", function (obj, prev) { //Define Sound Lists const downSounds = ["ouch", "bam", "smash", "pow", "thud", "argh", "wilhelm_scream"]; const upSounds = ["yay", "heal", "cheer", "not_today", "good", "stand_up", "I_feel_better"]; const downRandom = Math.floor(Math.random() * downSounds.length); const upRandom = Math.floor(Math.random() * upSounds.length); //Bar goes down if (prev.bar1_value > obj.get("bar1_value")) { playSound(downSounds[downRandom], 'play'); } // Bar goes up if (prev.bar1_value < obj.get("bar1_value")) { playSound(upSounds[upRandom], 'play'); }     }); }); I haven't tested it, but if it gives problems let me know. I re-ordered it a bit to make the sound lists easier to find and edit.
keithcurtis said: Try this: on('ready', () => {     let playSound = function (trackname, action) {         let track = findObjs({ type: 'jukeboxtrack', title: trackname })[0];         if (track) {             track.set('playing', false);             track.set('softstop', false);             if (action == 'play') {                 track.set('playing', true);             }         }         else {             sendChat('Damage Sound', '/w gm No Track Found...');             log("No track found " + trackname);         }     }     on("change:graphic:bar1_value", function (obj, prev) { //Define Sound Lists const downSounds = ["ouch", "bam", "smash", "pow", "thud", "argh", "wilhelm_scream"]; const upSounds = ["yay", "heal", "cheer", "not_today", "good", "stand_up", "I_feel_better"]; const downRandom = Math.floor(Math.random() * downSounds.length); const upRandom = Math.floor(Math.random() * upSounds.length); //Bar goes down if (prev.bar1_value > obj.get("bar1_value")) { playSound(downSounds[downRandom], 'play'); } // Bar goes up if (prev.bar1_value < obj.get("bar1_value")) { playSound(sounds[upRandom], 'play'); }     }); }); I haven't tested it, but if it gives problems let me know. I re-ordered it a bit to make the sound lists easier to find and edit. It did not seem to work, this is the error message I get. Your scripts are currently disabled due to an error that was detected. Please make appropriate changes to your script's code and click the "Save Script" button. We will then attempt to start running the scripts again.  More info...  If this script was installed from the Mod Library, you might find help in the Community API Forum. For reference, the error message generated was:  ReferenceError: sounds is not defined ReferenceError: sounds is not defined at apiscript.js:14101:27 at eval (eval at <anonymous> (/home/node/d20-api-server/api.js:168:1), <anonymous>:65:16) at Object.publish (eval at <anonymous> (/home/node/d20-api-server/api.js:168:1), <anonymous>:70:8) at TrackedObj.set (/home/node/d20-api-server/api.js:1099:14) at updateLocalCache (/home/node/d20-api-server/api.js:1442:18) at /home/node/d20-api-server/api.js:1628:11 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)
1681066233

Edited 1681066271
keithcurtis
Forum Champion
Marketplace Creator
API Scripter
I missed changing a variable name. This time I was in a place where I was able to test. It seems to be functioning OK. I have edited the code in my post above. The changed line was: playSound(sounds[upRandom], 'play'); to playSound(upSounds[upRandom], 'play'); Remember to edit the list of sounds to sounds that are actually installed in your jukebox, exactly as entered.
That seemed to work great! Thanks again!