Can you post the whole script? Did you use UnitScript or CreatureScript like you should?
Try printing at the start of the function for example with
std::cout << “TEST MESSAGE” << std::endl;
and it should print to the worldserver.exe console and you can see if it executes at all. Another option could be debugging.
What does “not work” mean? Does the code not execute at all or was something else the issue?
So unitscript is useless in both player and creaturescripts. Instead maybe try simply UnitScript?
Otherwise I think this may need separate calls to the hook for both player and creature or some other workaround.