(Twine 2.4.1, Sugarcube 2.36.1)

Hello all.
I’m working on a dungeon crawler and I’d like to make dice rolls against the character stats to decide success or failure. A simple “roll 1d100, check against relevant stat, if equal or lower, success. If higher, failure”.

So I got it working with a very simple:

``````<<link [[Roll vs Strength|Roll Result]]>>
<<set \$rollstr to random(1, 100)>> <</link>>
``````

And on the directed passage:

``````<<if \$rollstr gt \$str>> Failure!
<<else>> Sucess!
<</if>>
``````

And that’s all working well, apparently (of course, if anyone has a better solution, I’d be glad to see it).

Now, what if I wanted to complicate things and use the D&D Advantage/Disadvantage, i.e roll twice and pick the higher/lower result and then check against the stat? How would I do it?

Thank you all,
Fenrir

1 Like

Hi!

You can use Math.max and Math.min to get the higher/lower of two rolls:

``````<<set \$roll to Math.min(random(1,100), random(1,100))>> --> this gives you the lower of 2 rolls

<<set \$roll to Math.max(random(1,100), random(1,100))>> --> this gives you the higher of 2 rolls
``````

Also, I don’t really know your story structure, so this may not be useful to you, but if there’s significant branching depending on the results of such tests (and not just different flavour text) doing all the testing inside the `<<link>>` macro and redirecting to different passages may help organise the things better. It lets you use temporary variables as well, which don’t clutter your Story History:

`<<link "Test Strength">><<set _roll to Math.min(random(1,100), random(1,100))>><<if _roll gt \$str>><<goto "lose">><<else>><<goto "win">><</if>><</link>>`

2 Likes

Hello, and thank you for the answer.

The second code for some reason didn’t work, every time it resulted in “failure” even when it shouldn’t be possible (rolling 1d00 against 100, for example). But once I changed the temporary variable to a permanent one (the set _roll to set \$roll) it worked well.

Thank you for your time and the help.
Take care!

Fenrir

2 Likes