Here is the function I use in ScriptCards to roll on a rollable table. Pass it a table name and it will generate a roll on the table, taking the weighting of items in the table into account. It returns an array where the first item is the text of the entry and the second is the URL of the image. If the table doesn't exist, it will return an array with two empty strings. function rollOnRollableTable(tableName) {
var theTable = findObjs({type: "rollabletable", name:tableName })[0];
if (theTable !== undefined) {
var tableItems = findObjs({type: "tableitem", _rollabletableid: theTable.id});
if (tableItems !== undefined) {
var rollResults = {};
var rollIndex = 0;
var lastRollIndex = 0;
var itemCount = 0;
var maxRoll = 0;
tableItems.forEach(function(item) {
var thisWeight = parseInt(item.get("weight"));
rollIndex += thisWeight;
for (var x = lastRollIndex+1; x <= rollIndex; x++) {
rollResults[x] = itemCount;
}
itemCount += 1;
maxRoll += thisWeight;
lastRollIndex += thisWeight;
});
var tableRollResult = randomInteger(maxRoll);
return [tableItems[rollResults[tableRollResult]].get("name"),tableItems[rollResults[tableRollResult]].get("avatar")];
} else {
return ["",""];
}
}
}