Most up to date version is here: <a href="https://app.roll20.net/forum/permalink/11219976/" rel="nofollow">https://app.roll20.net/forum/permalink/11219976/</a> Hello Roll20! I haven't posted on the forums for a bit so I hope I've got this in the right place. I've written a small script to handle wild magic surges for the wild magic sorcerer in my 5e game, inspired by/based on the magicalsurges.js script by Nic. The script detects whether the caster of the spell is a wild magic sorcerer when they cast a spell and rolls a d20. If the d20=1 then it rolls on the WildSurges table using The Aaron's recursive tables mod. If you don't have a WildSurges table, it adds one for you to populate - shamelessly plagarised from Nic's script. It probably has its bugs, and ive not shared a script before, but it seems to work so I figured I'd share it. Suggestions for improvements welcome - my js is kinda rusty. //Script - WildSurges.js
//Version - v1.1
//Author - Lawmonger
//Based on MagicalSurges.js by nmrcarroll
on('ready',function(){ // check for state to store our sorcerers.
surgeTable=findObjs({ // Is there a WildSurges rollable table?
type: 'rollabletable',
name: 'WildSurges',
})[0];
if(!surgeTable){ //if no WildSurges table make one
surgeTable=createObj('rollabletable',{
name: 'WildSurges',
});
createObj('tableitem',{ //populate new WildSurges table with temporary entry
name: 'DELETE ME WHEN YOU HAVE ADDED YOUR OWN SURGES',
rollabletableid: surgeTable.id,
});
}
});
on("chat:message",function(msg){
var charID=(findObjs({
type:'character',
name: (msg.content.match(/charname=([^\n{}]*[^"\n{}])/) || [])[1], //get the msg sender's character ID from roll template
})[0] || {id: 'API',}).id; //if no char. ID, ID as API
if (charID!=='API'){ //if message is not from the API
var subclass = getAttrByName(charID,"subclass"); //get the character's subclasses
var mc1subclass = getAttrByName(charID,"multiclass1_subclass"); //
var mc2subclass = getAttrByName(charID,"multiclass2_subclass"); //ALL OF THEM!
var mc3subclass = getAttrByName(charID,"multiclass3_subclass"); //
if (subclass||mc1subclass||mc2subclass||mc3subclass=="Wild Magic"){ //if the subclass is Wild Magic
var spellLevel=msg.content.match(/spelllevel=([^\n{}]*[^"\n{}])/); //check the roll template for spell level
if(spellLevel!==null){ //if there is a spell level field
spellLevel=Number(spellLevel[1]); //get the numerical value and treat it as a number
}
var cantrip = msg.content.includes('cantrip}}'); //is it a cantrip?
var spellTemplate = msg.rolltemplate == 'spell'; //is it using the spell roll template?
var roll=0; //initialise roll value as 0
var outputMsg =''; //initialise blank message
if(!cantrip && (spellLevel || spellTemplate)){ //if the msg is a spell and not a cantrip
roll = randomInteger(20); //roll a d20
outputMsg = `${roll}`; //set the output message to report the roll
if(roll==1){ //if the roll == 1,
sendChat("WILD SURGE!",`!rt /em [[1t[WildSurges] ]]`); //emote WILD SURGE! to chat & call recursive tables mod to roll on WildSurges
};
sendChat("Wild Roll",`${outputMsg}`); //send output message (d20 roll) to chat.
}
}
}
});