Trying to display a dice-roll range of results

I’ve used code similar to that of below to great effect in relation to random dice-rolls in relation to variables / arrays.

However now I’m trying to get the dice to roll in a ‘range’ of results from 0-30, 31 - 62, 63 - 100 etc

I try going for a simple dual result with the below option but to no avail. I get incorrect ‘if’ statements.


Roll for a critical result! TEST!
<span id='dice-outcome'></span>
<span id='dice-button'><<button 'Critical Roll1!'>>
	<<set $roll to random(0, 100)>>
	<<if $roll (0, 30)>>
	<<replace '#dice-outcome'>>
	Collision!<br>
	<<replace '#dice-button'>><</replace>> /% remove button %/
	<</replace>><</if>>
	<<if $roll (31, 100)>> 
		<<replace '#dice-outcome'>>
			Miss!
			That was a close call!
		<</replace>><</if>>
	<<replace '#dice-button'>><</replace>> /% remove button %/
<</button>>
</span>```

Please can anyone help?

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>```

Note: I have not tested this…

Thanks and it does works!

But I’m going trying so multiple possiblities come up using elseifs. Here’s what I came up with…

<span id='dice-button'><<button 'Critical Roll1!'>>
	<<set $roll to random(0, 99)>>
	<<if $roll lt 30 >>
	<<replace '#dice-outcome'>>
	Close miss!<br>
	<<replace '#dice-button'>><</replace>> /% remove button %/
	<</replace>>
	<<elseif $roll gt 31 >> 
	<<replace '#dice-outcome'>>
	Collision!<br>
	<<elseif $roll gt 61>> 
	<<replace '#dice-button'>><</replace>> /% remove button %/
	<</replace>>
	<<replace '#dice-outcome'>>
			Severe Collision!
		<</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…

Okay Josh, I’ll give that a whirl next.

<span id='dice-button'><<button 'Critical Roll1!'>>
	<<set $roll to random(0, 99)>>
	<<if $roll lt 30 >>
	<<replace '#dice-outcome'>>
	Close miss!<br>
	<<replace '#dice-button'>><</replace>> /% remove button %/
	<</replace>>
	<<elseif $roll lt 61 >> 
	<<replace '#dice-outcome'>>
	Collision!<br>
	<<else $roll lt 91>> 
	<<replace '#dice-button'>><</replace>> /% remove button %/
	<</replace>>
	<<replace '#dice-outcome'>>
			Severe Collision!
		<</replace>><</if>>
	<<replace '#dice-button'>><</replace>> /% remove button %/
<</button>>
</span>

Getting an error again on the use of else with the if.

I tried this one.

<span id='dice-outcome'></span>
<span id='dice-button'><<button 'Perception Test!'>>
	<<set $roll to random(0, 100) + $rollModifier>>
	<<if $roll lt 30 >>
	<<replace '#dice-outcome'>>
	You rolled $roll!
You fail to notice anything.<br>
	<<replace '#dice-button'>><</replace>> /% remove button %/
	<</replace>><</if>>
	<<if $roll lt 60>> 
		<<replace '#dice-outcome'>>
			Your rolled $roll!<br><br>__SUCCESS!__

You sense a presence in the air...
		<</replace>><</if>>
	<<replace '#dice-button'>><</replace>> /% remove button %/
	<if $roll lt 99>>
	<<replace '#dice-outcome'>>
	You see it!<</replace>><</if>>
	<<replace '#dice-button'>><</replace>> /% remove button %/
	
<</button>>
</span>

Still no dice and getting `if` errors.

Yeah, the else doesn’t take a condition. It just catches everything “else”. So it’s not <<else $roll lt 91>>, it’s just <<else>>.

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>>
1 Like

I get ‘Error childtag was found outside of a call to its parent’


<span id='dice-outcome'></span>
<span id='dice-button'><<button 'Test!'>>
	<<set $roll to random(0, 100) + $rollModifier>>
	<<if $roll lt 30 >>
	<<replace '#dice-outcome'>>
	You rolled $roll!
You fail to notice anything.<br>
	<<replace '#dice-button'>><</replace>> /% remove button %/
	<</replace>><</if>>
	<<elseif $roll lt 61>> 
		<<replace '#dice-outcome'>>
			Your rolled $roll!<br><br>__SUCCESS!__

You sense a presence in the air...
		<</replace>><</if>>
	<<replace '#dice-button'>><</replace>> /% remove button %/
	<else>>
	<<replace '#dice-outcome'>>
	$roll<br>You see it!<</replace>><</if>>
	<<replace '#dice-button'>><</replace>> /% remove button %/
	<</button>>
</span>

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>>.

Ok just tried closing it with an if at the end and:

Error: child tag <</if>> was found outside of a call to its parent macro <<if>>

<span id='dice-outcome'></span>
<span id='dice-button'><<button 'Test!'>>
	<<set $roll to random(0, 100) + $rollModifier>>
	<<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>>
	<<replace '#dice-button'>><</replace>> /% remove button %/
	<</button>><</if>>
</span>

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.

1 Like

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>
1 Like