How can I add an item by the variable?

I wanted to add certain objects depending on the value of a certain variable, but when trying to make a script for the event, I got an error.
In this case the script was:
$game_party.gain_item($data_items[$game_variables[5]], 1)​

What am I doing wrong? Or how to do it correctly?
if you use scriptcall for it and that fails (works in MV) try this:

gv = $game_variables[5]
$game_party.gain_item($data_items[gv], 1)

I cannot say that this can work, but worth a shot as I dont know ruby code or
whatever vx ace codes uses.
Your script looks good, but it seems you have somehow added a non-printed character at the end that messes with the Ruby interpreter.
if you use scriptcall for it and that fails (works in MV) try this:

gv = $game_variables[5]
$game_party.gain_item($data_items[gv], 1)
Yes, it worked
Thank you very much for the help n.n
was a wild guess though, I know more JS than Ruby, but glad it worked out.

Happy game making 🙂

Original thread: https://forums.rpgmakerweb.com/threads/how-can-i-add-an-item-by-the-variable.167386/