Skip to content

Instantly share code, notes, and snippets.

@JoshWright22
Last active January 25, 2026 01:17
Show Gist options
  • Select an option

  • Save JoshWright22/fcb1b38e77dbc02272cac967f742a94a to your computer and use it in GitHub Desktop.

Select an option

Save JoshWright22/fcb1b38e77dbc02272cac967f742a94a to your computer and use it in GitHub Desktop.
//My solution for 5e current foundry vtt, modified by me from source https://gist.github.com/xdy/bdd08e101fc390016dd432f28c4abfdb
// 1. Filter for valid 5e tokens
const tokens = canvas.tokens.controlled.filter(t =>
t.actor && (t.actor.type === 'character' || t.actor.type === 'npc')
);
if (tokens.length === 0) {
return ui.notifications.warn("Please select at least one Character or NPC token.");
}
// 2. Ensure Combat exists
let combat = game.combat;
if (!combat) {
combat = await Combat.create({scene: canvas.scene.id, active: true});
}
// 3. Add tokens correctly via toggleCombat to satisfy modules like Monk's
for (let t of tokens) {
if (!t.inCombat) {
await t.toggleCombat();
}
}
// 4. Identify combatants that need an initiative roll
// We filter combatants linked to our selected tokens
const combatantIdsToRoll = combat.combatants
.filter(c => tokens.some(t => t.id === c.tokenId) && c.initiative === null)
.map(c => c.id);
// 5. Roll initiative for those specific IDs
if (combatantIdsToRoll.length > 0) {
await combat.rollInitiative(combatantIdsToRoll, { skipDialog: true });
} else {
ui.notifications.info("All selected tokens are in combat and have initiative.");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment