Auto-translated
You would be better off listening to advice and fixing obvious errors instead of saying that something works somewhere. The function receives the hero variable, so it should be used further. Somewhere else in the code, you accidentally created a global variable named heroname and assigned it the name of the main hero. That's why, during testing, it seemed to you that this function worked as it should. But it's just a coincidence that can disappear at any moment.
I described debugging methods in my FAQ. To make the console work at full capacity, you need to use debug messages. Before/after/inside the function, before/after the trigger.
As they say, you live and learn.