String Variable to Integer

Hello everyone, having an issue while trying to work with a variable.
I am reading the value from the enemy notetag with a regex and saving the value to a global variable, then using said value to apply a state on an enemy, but I get an error in the state aplying part.

My script is as follows:
enemy = $game_troop.members[$game_variables[2]]
$game_variables[109] = enemy.data_battler.note =~ /<Element = (\d+)>/i ? $1 : nil
enemy.add_state($game_variables[109])

The variable 2 is assigned to a loop that goes from 0 to 7, and breaks when it reaches 7 (in my game the max amount of enemies per troop is 8)
Then it reads the Element notetag from the enemy and assigns that value to variable 109, and then it assigns a state corresponding to the same number, however I get an error saying it cannot convert string to integer.

How can I save the notetag value as integer, or change the value in variable 109 to integer so I can work with it??
Thank you in advance!
I don't see how that will work. I don't think there is a data_battler in the program. unless you have a script that has that.
Changing values is not hard as long as the value is a number for integer.
$game_variables[109].to_i
.to_s -> string
.to_f -> float with 2 decimal numbers -> 0.01
.to_i -> integer
.to_a -> array
I think there is more, but you get the idea.
I don't see how that will work. I don't think there is a data_battler in the program. unless you have a script that has that.
Changing values is not hard as long as the value is a number for integer.
$game_variables[109].to_i
.to_s -> string
.to_f -> float with 2 decimal numbers -> 0.01
.to_i -> integer
.to_a -> array
I think there is more, but you get the idea.
Yeah, the data_battler part is handled with the combat script, that lets me use the enemy index, so far that part works, the error always shows when applying the state.

Changed the script with what you mentioned to this:
enemy = $game_troop.members[$game_variables[2]]
$game_variables[109] = enemy.data_battler.note =~ /<Element = (\d+)>/i ? $1 : nil
$game_variables[109].to_i
enemy.add_state($game_variables[109])

Still getting the error.

Breaking down the script I get this:
enemy = $game_troop.members[$game_variables[2]]
$game_variables[109] = enemy.data_battler.note =~ /<Element = (\d+)>/i ? $1 : nil
1625869405243.png

Which is correct, as the enemy notetag is:
<Element = 2>

But if I try to add the state using the stored value in variable 109 I get the error:
1625869565365.png
Ok, so the value should be an integer anyways.
But you don't do $game_variables[109].to_i
instead use the last call
enemy.add_state($game_variables[109].to_i)
So if somehow the variable is saving the number as a string then this will change the value to integer and apply that to the command add_state. giving a correct script call.
What might also be wrong is that you have nil in the code.
Change that to 0 and see what happens. then change the code you have for .to_i
Ok, so the value should be an integer anyways.
But you don't do $game_variables[109].to_i
instead use the last call
enemy.add_state($game_variables[109].to_i)
So if somehow the variable is saving the number as a string then this will change the value to integer and apply that to the command add_state. giving a correct script call.
What might also be wrong is that you have nil in the code.
Change that to 0 and see what happens. then change the code you have for .to_i
1625871274641.png

Awesome it worked with the value changing, I wasn't putting it in the correct place as you mentioned, I am converting it to a local variable and changing to integer while adding 60 it. (Elements go from 0 to 8, but the states corresponding to them go from 60 to 68).

By any chance, do you know the script to fetch the troop size in the battle?

Thanks a lot!

Edit:
Nvm, it's $game_troop.alive_members.length
Thanks again for the support

Original thread: https://forums.rpgmakerweb.com/threads/string-variable-to-integer.138319/