A dice roller!
A dice roller script you can copy paste into your own scenarios. Use the syntax >roll xdy+z to trigger a roll. For example. "You fire your bow at the enemy. >roll 1d20+8 to see if you hit, if so >roll 1d8+4 for damage." Ensure you add something along the lines of "Rolls are meta game and are not actual actions taken by the player's character. Use dice rolls, when provided to determine the outcome of the player character's actions." Copy the following: (Don't forget to leave a like!) // Dice roller script for AI Dungeon const modifier = (text) => { // Match multiple roll commands in the text, including modifiers (e.g., XdY+Z or XdY-Z) const rollPattern = /(\d+)d(\d+)([+-]\d+)?/gi; let matches; let messages = []; // Process all matches while ((matches = rollPattern.exec(text)) !== null) { const numDice = parseInt(matches[1]); // Number of dice const dieType = parseInt(matches[2]); // Type of dice const modifier = matches[3] ? parseInt(matches[3]) : 0; // Modifier, default to 0 if not present if (numDice > 0 && dieType > 0) { // Roll the dice and calculate the sum let rolls = []; for (let i = 0; i < numDice; i++) { rolls.push(Math.floor(Math.random() * dieType) + 1); } const rollTotal = rolls.reduce((a, b) => a + b, 0); const total = rollTotal + modifier; // Create a result message for this roll messages.push(`🎲 ${numDice}d${dieType}${modifier !== 0 ? (modifier > 0 ? `+${modifier}` : `${modifier}`) : ""}: [${rolls.join(", ")}] ${modifier !== 0 ? `+ ${modifier} =` : "="} ${total}`); } else { // Handle invalid inputs messages.push("❌ Invalid roll command detected."); } } // Combine and display all results if any rolls were found if (messages.length > 0) { state.message = messages.join("\n"); } // Let the action proceed as normal return { text }; }; // Don't modify this part modifier(text);
