I think `random(0,100) will give a random number from 0 to 100 inclusive, i.e., from 101 possible numbers, so I have done this as 0 to 99. Then used if the roll is under 30 or not.
<span id='dice-button'><<button 'Critical Roll1!'>>
<<set $roll to random(0, 99)>>
<<if $roll lt 30 >>
<<replace '#dice-outcome'>>
Collision!<br>
<<replace '#dice-button'>><</replace>> /% remove button %/
<</replace>>
<<else>>
<<replace '#dice-outcome'>>
Miss!
That was a close call!
<</replace>><</if>>
<<replace '#dice-button'>><</replace>> /% remove button %/
<</button>>
</span>```
That’ll give you problems, because $roll gt 31 will catch everything else, so the $roll gt 61 condition will never fire. It’s better to keep all your conditions in the same direction (all less-than or all greater-than) and work from one end to the other.
Also in this case you’re missing 30 and 31 entirely, because they are neither less than 30 nor greater than 31. I suspect you probably want to check less-than 30, then less than 60, then use an <<else>> for the rest…
Ok, but I want it to display three or more results in the click of the button.
I can’t really see how to do that using the <> and no condition to go with it.
Sorry, I don’t quite understand that… It seems like maybe you’re trying to do two things at once and getting them mixed up? The structure of the if statement to divide up the ranges should look like this. So get that right first. Then you can put whatever you want inside each section.
<<if $roll lt 31>>
0 to 30
<<elseif $roll lt 61>>
31 to 60
<<else>>
everything else: so 61 to 99
<</if>>
You’re closing the <<if>> (with <</if>>) for every range: you should only close it once at the end. You should have an <<if>> ... <</if>>, and then you can put as many <<elseif>>s in between and one <<else>> in there (but it has to be the last one).
<<elseif condition>> and <<else>> are “child tags”: they’re only allowed inside the <<if>>...<</if>>.
Almost there: you have <else>>: it’s missing one of the < at the beginning. I think that’s the only thing wrong here…
A lot of this coding stuff is just writing things exactly and noticing the little changes. There’s one guy (at least) who made a whole thing about writing beginner coding books that are just exercises in typing things exactly and finding the mistakes in deliberately slightly wrong examples.
Good eye, thanks for that Josh, was almost at the limit of my patience with the coding dance there.
I had to move the closing <> a little bit, so the end code that works like this:
<span id='dice-outcome'></span>
<span id='dice-button'><<button 'Test!'>>
<<set $roll to random(0, 100)>>
<<if $roll lt 30>>
<<replace '#dice-outcome'>>
You fail to notice anything.<br>
<<replace '#dice-button'>><</replace>> /% remove button %/
<</replace>>
<<elseif $roll lt 61>>
<<replace '#dice-outcome'>>
You sense a presence in the air...
<</replace>>
<<replace '#dice-button'>><</replace>> /% remove button %/
<<else>>
<<replace '#dice-outcome'>>
You see it!<</replace>><</if>>
<<replace '#dice-button'>><</replace>> /% remove button %/
<</button>>
</span>
You don’t need to replace #dice-button multiple times, once at the end would be sufficient. For example: (whitespace added for clarity)
<span id='dice-outcome'></span>
<span id='dice-button'>
<<button 'Test!'>>
<<set $roll to random(0, 100)>>
<<if $roll lt 30>>
<<replace '#dice-outcome'>>
You fail to notice anything.
<</replace>>
<<elseif $roll lt 61>>
<<replace '#dice-outcome'>>
You sense a presence in the air…
<</replace>>
<<else>>
<<replace '#dice-outcome'>>
You see it!
<</replace>>
<</if>>
<<replace '#dice-button'>><</replace>>
<</button>>
</span>
You could also do something similar for #dice-outcome. For example: (whitespace added for clarity)
<span id='dice-outcome'></span>
<span id='dice-button'>
<<button 'Test!'>>
<<set $roll to random(0, 100)>>
<<replace '#dice-outcome'>>
<<if $roll lt 30>>
You fail to notice anything.
<<elseif $roll lt 61>>
You sense a presence in the air…
<<else>>
You see it!
<</if>>
<</replace>
<<replace '#dice-button'>><</replace>>
<</button>>
</span>