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

Sound effect on token drop

How can you make a sound to play each time you drop a specific token on a map?
1733237289
keithcurtis
Forum Champion
Marketplace Creator
API Scripter
If you want it to be done automatically, it could be done by a mod script. Here's an example from the wiki, with comments where code could be added: on("add:graphic", function(obj) {   //Will be called for all new graphics, including ones that already existed at the start of the play session. }); var started = false; on("ready", function() {   on("add:graphic", function(obj) {     //Test obj to see if it meets the criteria (probably a name).     //Jukebox code to play sound.   });   //You could also set a variable and ignore add events until it is true.   started = true; });
1733239823

Edited 1733239872
DM
Pro
Many thanks although I already figured it out. Now I have to filter the types of tokens this applies (the base function I created applies globally). I think your comment 'Test obj to see if it meets the criteria (probably a name)' is what I need. How would I implement this? I have made custom tokens with a number indicating damage suffered in the center and I would like those and only those to come with a certain sound when dropped on screen (so the player may see how much damage he took and add some drama to it). So I need a list of exclusions, the token names separated by commas I suppose?
1733489650

Edited 1733489669
DM
Pro
The following script plays a track named 'TrackName' every time something is placed on the battlefield: on('ready', () => { on("add:graphic", function(obj) { sendChat('', '!sfx volume:100 action:play song:TrackName') }); }); Now the question is how to add specifications so it plays only with certain tokens or to have different tracks for different types of tokens.
1733501728

Edited 1733502166
The Aaron
Roll20 Production Team
API Scripter
You need some criteria on the graphic that you can filter on.  You could use the graphic's URL (or a subset of it) to match those specific graphics, but that's tedious to maintain and debug.  Better would be to make a deck and have those tokens as a deck of cards, then play the sound only if cards from that deck are played to the tabletop.  You can then add/remove/replace those tokens with impunity and not need to modify the script.  Also, you can just draw all the cards from the deck and have a convenient place to pull all those tokens onto the table from.  Cleanup becomes pretty easy also, you just recall all the cards. To do that, inside your on('ready',...) call, you'd find the deck, then load all the cards from the deck and store all their ids.  Then you can just reference them when a graphic is added. Something roughly like this (typed from memory and not tested at all) code: on('ready', () => { let DamageCardIds; let deck = findObjs({type:'deck',name:'Damage Cards'})[0]; if(deck){ DamageCardIds = findObjs({type:'card',deckid: deck.id}).map(c=>c.id); } on("add:graphic", function(obj) { if(DamageCardIds.includes(obj.get('cardid')){ // you don't really need to call another script here, you could just start the track directly. sendChat('', '!sfx volume:100 action:play song:TrackName') ; } }); });
1733660374

Edited 1733672926
DM
Pro
Interesting approach although it will be hard to pull off because it means I will have to have 70+ images pre-loaded and placement will be delayed (what I was trying to avoid). But it gives me an idea - roll20 allows for folders, if I can set the sound to play for all tokens inside a specific folder (using the folder's ID - I suppose there is one) it will make it all the more easier and faster.