Help with Roll Dice system

If I may expand on Josh’s answer, I wrote this little dice rolling function a while back and I think it’s what you’re looking for. Following the same advice I gave on adding the function to your script here, just add this function:

window.rollDice = function(die) {
	var dice = die.toUpperCase().split("D");
	var plus = dice[1].split("+");
	if (plus.length > 1) {
		dice[1] = plus[0];
		plus = parseInt(plus[1]);
	}
	else plus = 0;
	dice[0] = parseInt(dice[0]);
	dice[1] = parseInt(dice[1]);

	var total = 0;
	var a;
	for (a = 0; a < dice[0]; ++a) {
		total += random(1, dice[1]);
	}
	total += plus;
	return total;
};

Then in your story script do what Josh suggested

<<set $STR = 2>>
<<linkreplace "Roll the dice">>
<<set _roll to rollDice("3D6") + $STR>>
You got: _roll
You needed: 15
<<if _roll >= 15>>[[passage B]]
<<else>>[[passage C]]
<</if>>
<</linkreplace>>

My function also allows for hard coding bonuses into what you pass it, so you could do rollDice("2d10+5") and that’d work as you expected. The “d” can be upper or lower case also.

2 Likes