Problem adding character age

The problem I have with the following code is it does not seem to save the number entered into the prompt box, does anybody know how I can fix this?
Spoiler: Code
Code:
  ◆Script:ageS = $gameVariables.value(29);
  :      :var ageS = prompt("Enter a Value", "0");
  :      :if(isNaN(ageS)){
  :      :$gameMessage.setFaceImage('Actor1',0);
  :      :$gameMessage.setBackground(1);
  :      :$gameMessage.setPositionType(1);
  :      :$gameMessage.add("This isn't a number");
  :      :$gameVariables.setValue(35, 0);
  :      :}else{
  :      :var ageI = parseInt($gameVariables.value(29));
  :      :$gameVariables.setValue(35, ageI);
  :      :}
why use a script for this? there is a number input event command....
Prompt always returns a string, which means your if(isNaN(ageS)) will always be true. You need to move your parseInt function above your if statement and then use if(isNaN(ageI)).
Well, I'm amazed you managed to put 12 lines of code in there, since the capacity of script field is only 10 lines of code.
However, in all seriousness, it looks like you are confused about javascript a bit.
So let me say what the code you did does:
1. defines new global variable ageS and sets its value to variable 29's value
2. defines new local variable ageS and sets its value to prompt input
3. checks if local variable ageS is NaN, which it will never be, because it is a string
4. ageI takes value from variable 29, which has been COMPLETELY UNCHANGED through the whole code
5. sets variable 35 equal to variable 29.

There are also two things. One of them is reference, the other one is assignment. It has to do with mutable (can be changed) and immutable things. Mutable get referenced, immutable get assigned.
For example a number is immutable. You cannot redefine 3. Strings are immutable. You can use methods like push etc., but in practice that will create a new string, it will not change the current one. Null and undefined are immutable. So if you set ageS equal to $gameVariables.value (29), if $gameVariables.value(29) is equal to a number, string, null, undefined or whatever immutable thing, it means that no matter how much you change ageS from now on, variable 29 will NOT be changed. And no matter how you change variable 29, ageS will not be changed either.

So hoping that ageS = $gameVariables.value(29) and then ageS = prompt (whatever) changes somehow value of variable 29 is kind of a waste of time... It will not.

If you want to save a prompt input to variable 29, you'd go at it like this:
$gameVariables.setValue (29, prompt("Enter a value", "0"));
No need for more than that.


Prompt always returns a string, which means your if(isNaN(ageS)) will always be true
No, that is wrong. NaN is a special value that signifies illegal operations regarding numbers. For example Number("ag6") returns NaN, because "ag6" is impossible to be converted to number. 6 * "3" returns NaN, because you can't multiply number with a string. And isNaN simply checks if the variable value is explicitly equal to NaN. In other words it is equivalent to var.typeOf (NaN).
@Poryg whoops, you're right. That's what I get for answering questions before breakfast. 😛
@Poryg I did pretty much what you said but now get an error saying Unexpected syntax ;
Spoiler: Code
Code:
  ◆Script:$gameVariables.setValue(29, prompt("Enter a value", "0");
  :      :$gameVariables.setValue(35, $gameVariables.value(29));
Sorry I just noticed the massive glaring problem. No ) at the end
Yup.
Well, with this code you have a pretty useless step there 😀
So you can simplify it even further:
label: age
Script: $gameVariables.setValue (35, Number(prompt("Enter a value", "0")));
conditional branch - script - isNaN($gameVariables.value(35))
jump to label: age
that is a very helpful tip and yes it did work as you said.
I may implement that if not using the other variable to track age change.

Original thread: https://forums.rpgmakerweb.com/threads/problem-adding-character-age.90489/