# NaN variable problem [Sugarcube 2]

closed

Hello guys, I'm having quite a problem at the moment and I was not able to figure out how to solve it by myself, so after trying a lot of different ways to make it work, there is nothing I can do, so I would like to request some help. Thanks beforehand!

So here is the code:

``````<<set \$vest = 5>>
<<set \$variable1 = 100>>
<<set \$moralpueblo = 45>>

<<set \$variable02 = \$moralpueblo / 2>>
<<set \$variable03 = \$vest * 3>>

<<set \$fuerzatropas = \$fuerzatropas1 / \$variable1>>
<<set \$fuerzatropas1 = \$variable02 + \$variable03 + \$tropas>>``````

By the way, \$vest is just an abbreviation of two spanish words: ventaja estratégica wich means estrategic advantage

closed with the note: Question answered

by (159k points)
selected by

You are trying to use the value of the \$fuerzatropas1 story variable in a mathematical expression ...

``<<set \$fuerzatropas = \$fuerzatropas1 / \$variable1>>``

... before you assigned it a numerical value in the next line. The same is true for your \$tropas story variable which also hasn't been assigned a numerical value.

By default all SugarCube story versions story variables start as undefined and using an undefined story variable within a mathematical expression will result in a Not-a-Number error, because the value of the undefined variable isn't a number.

To solve your issue you need to first assign an initial value to the \$tropas variable and then change the order of the last two lines in your example...

``````<<set \$fuerzatropas1 = \$variable02 + \$variable03 + \$tropas>>
<<set \$fuerzatropas = \$fuerzatropas1 / \$variable1>>``````

... so the value of \$fuerzatropas1 is calculate before you reference it in the other mathematical expression.

by (430 points)
Oh my god, I did such a stupid mistake... Thanks for answering the question!
by (159k points)
If you are happy with this answer could you please use the relevant option/button to mark it as 'answered' (or whatever the actual state is named), that way it's associated icon will change from blue to green and others will know that the question contains an acceptable answer.

It also means that you can then remove the [solved] text from the Question Title.

I also forgot to ask you to please use the Question Tags to state the name and version full number of your Story Format, instead of doing that within the Question Title. It makes easier to search for questions related to a particular Story Format.