Having issues getting this monster encounter code to work (Twine2/Sugarcube2)[Solved]

The issue it this:

<<set $chance to random(1, 100)>>
<<if $chance gte 90>>
	<<set $enemyEncounter to true>>
	<<set $ename to "Slime">>
	<<set $ehp to 90>>
	<<set $emaxhp to 90>>
	<<set $eAff to 0>>
	<<link "You are attacked by a slime!" "Combat!">><</link>>
<</if>>
<<if $chance is random(80, 89)>>
	<<set $enemyEncounter to true>>
	<<set $ename to "Flauna">>
	<<set $ehp to 95>>
	<<set $emaxhp to 95>>
	<<set $FlaunaAff to 50>>
	<<link "You are attacked by an odd slime-girl." "Combat!">><</link>>
<</if>>
<<if $chance is random(70, 79)>>
	<<set $enemyEncounter to true>>
	<<set $ename to "Wolf Pack">>
	<<set $ehp to 125>>
	<<set $emaxhp to 125>>
	<<link "You are attacked by a pack of wolves" "Combat!">><</link>>
<</if>>
<<if $chance lte 69>>
	[[Keep Exploring?->Walk Around?]]
<</if>>

The two middle <> statements don’t appear as they should in the (walk around) passage. but the outer <> statements work perfectly. help please!

Edit: The endifs are there just unseeable in this page.
[Mod Edit to include code formatting markup]

1 Like

@Steampunkfire
You can use the </> button in the comment field toolbar (or the related hotkey combination) to wrap your code example within Preformatted Text markup, this will help with the lose of TwineScript end tags as well as stop the replacement of standard quotes (") with Typographical ones (“ ”) which are invalid when used as macro arguments.

Doing so would result in your example looking something like the following…

<<set $chance to random(1, 100)>>
<<if $chance gte 90>>
	<<set $enemyEncounter to true>>
	<<set $ename to "Slime">>
	<<set $ehp to 90>>
	<<set $emaxhp to 90>>
	<<set $eAff to 0>>
	<<link "You are attacked by a slime!" "Combat!">><</link>>
<</if>>
<<if $chance is random(80, 89)>>
	<<set $enemyEncounter to true>>
	<<set $ename to "Flauna">>
	<<set $ehp to 95>>
	<<set $emaxhp to 95>>
	<<set $FlaunaAff to 50>>
	<<link "You are attacked by an odd slime-girl." "Combat!">><</link>>
<</if>>
<<if $chance is random(70, 79)>>
	<<set $enemyEncounter to true>>
	<<set $ename to "Wolf Pack">>
	<<set $ehp to 125>>
	<<set $emaxhp to 125>>
	<<link "You are attacked by a pack of wolves" "Combat!">><</link>>
<</if>>
<<if $chance lte 69>>
	[[Keep Exploring?->Walk Around?]]
<</if>>

If I understand correctly you want:

  1. the first section of content to be only executed if $chance is greater than or equal to 90.
  2. the second section only if $chance is between 80 and 89 inclusive.
  3. the third section only if $chance is between 70 and 79 inclusive.
  4. the forth section only if $chance is less than or equal to 69.

The following example does this.

<<set $chance to random(1, 100)>>
<<if $chance gte 90>>
	<<set $enemyEncounter to true>>
	<<set $ename to "Slime">>
	<<set $ehp to 90>>
	<<set $emaxhp to 90>>
	<<set $eAff to 0>>
	<<link "You are attacked by a slime!" "Combat!">><</link>>
<<elseif $chance gte 80 and $chance lt 90>>
	<<set $enemyEncounter to true>>
	<<set $ename to "Flauna">>
	<<set $ehp to 95>>
	<<set $emaxhp to 95>>
	<<set $FlaunaAff to 50>>
	<<link "You are attacked by an odd slime-girl." "Combat!">><</link>>
<<elseif $chance gte 70 and $chance lt 80>>
	<<set $enemyEncounter to true>>
	<<set $ename to "Wolf Pack">>
	<<set $ehp to 125>>
	<<set $emaxhp to 125>>
	<<link "You are attacked by a pack of wolves" "Combat!">><</link>>
<<elseif $chance lt 70>>
	[[Keep Exploring?->Walk Around?]]
<</if>>

note: Some enhancements to the above.

  1. You could replace the final <<elseif $chance lt 70>> with an <<else>> if there are no other conditions.
  2. You could place the <<set>> macro calls related with each condition range within the associated <<link>> macro’s body, that way those <<set>> macros will only be executed after the end-user selects that link.
    Doing the above would result in code like the following…
<<set $chance to random(1, 100)>>
<<if $chance gte 90>>
	<<link "You are attacked by a slime!" "Combat!">>
		<<set $enemyEncounter to true>>
		<<set $ename to "Slime">>
		<<set $ehp to 90>>
		<<set $emaxhp to 90>>
		<<set $eAff to 0>>
	<</link>>
<<elseif $chance gte 80 and $chance lt 90>>
	<<link "You are attacked by an odd slime-girl." "Combat!">>
		<<set $enemyEncounter to true>>
		<<set $ename to "Flauna">>
		<<set $ehp to 95>>
		<<set $emaxhp to 95>>
		<<set $FlaunaAff to 50>>
	<</link>>
<<elseif $chance gte 70 and $chance lt 80>>
	<<link "You are attacked by a pack of wolves" "Combat!">>
		<<set $enemyEncounter to true>>
		<<set $ename to "Wolf Pack">>
		<<set $ehp to 125>>
		<<set $emaxhp to 125>>
	<</link>>
<<else>>
	[[Keep Exploring?->Walk Around?]]
<</if>>

It worked, thank you so much dude!