MZ Trying to write a common event to play a message when learning a skill

I'm trying to use an item to learn a skill. In MZ there is an option to add a skill on use, however the item doesn't display a message saying you have learned said skill. I want to create a common event (as generic as possible) that basically calls on the obtainskill message. This is what I tried as a common event. It doesn't give an error but does nothing when a skill book is used.

Game_Actor.prototype.displayObtainSkill = function(newSkills) {
const text = TextManager.obtainSkill.format(this._name, skill.iconIndex, skill.name);
}

Any help would be great.
Thanks
If you can't figure it out in Javascript, it would be incredibly easy to event~
There are a surprisingly large number of reasons why that script call didn't work for you. But before getting into how to fix it, it'd be helpful to have a better idea of exactly what you're trying to do. Can you please explain why you don't want to just use a "Show Text" event command, and just type "You have learned Blah Blah Blah" in it?

Is it that you intend to have a ton of these skill learning items, and you don't want to have to make a separate common event (or conditional branch) for each of them, or something like that?
@Arthran
I want to have an message show up when you use an item to learn a skill.

The reason I haven't used show text is because I dont want to create a massive amount of messages/events (ie one for each spell and each person who can learn each spell) As far as I now with show text there is no way to make an ambiguous message that will detect the actor and the skill and the skill icon. Which is what i have the obtainskill message set to show. So i was trying to call on the obtainskill message for item use skill learning.

@KawaiiKid
Full eventing of it is what i considered first. It works but its a lot of extra messages. I was hoping that i could use the learn skill effect on the item and trigger an ambiguous message so it would be handled much simpler.
Here, try this script call in your common event:

JavaScript:
const actor = $gameActors.actor($gameTemp.lastActionData(2));
const item = $dataItems[$gameTemp.lastActionData(1)];

for (const effect of item.effects) {
    if (effect?.code === Game_Action.EFFECT_LEARN_SKILL) {
        const skill = $dataSkills[effect.dataId];
        $gameMessage.add(`${actor.name()} has learned \\I[${skill.iconIndex}]${skill.name}.`);
    }
}

*** Note to anyone who stumbles across this thread and wants to try this code: This will only work for MZ. It would be a bit trickier to pull off in MV.
Here, try this script call in your common event:

JavaScript:
const actor = $gameActors.actor($gameTemp.lastActionData(2));
const item = $dataItems[$gameTemp.lastActionData(1)];

for (const effect of item.effects) {
    if (effect?.code === Game_Action.EFFECT_LEARN_SKILL) {
        const skill = $dataSkills[effect.dataId];
        $gameMessage.add(`${actor.name()} has learned \\I[${skill.iconIndex}]${skill.name}.`);
    }
}

*** Note to anyone who stumbles across this thread and wants to try this code: This will only work for MZ. It would be a bit trickier to pull off in MV.
Thank you very much! That is exactly what I was trying to do. My js code knowledge is not as good as I would like. Some of the simpler stuff I've been able to work out but this one was rough. I was looking at the effects section of the code and recognize come of the parts you have there.

Again, thanks for helping me get this fixed up.
After further testing, the only problem this creates is, it makes a consumable reuseable on the same target for some reason. Without the script call it auto prevents reusing a learn skill effect if they have the skill. In its current form it will be reusable and consume the item even if they already know the skill. For basic consumables that is still fine. For my purposes I need the reuse prevent.

I was able to convert it into a plugin that still makes the message appear and prevents the item reuse as intended for learn skills.

Game_Action.prototype.apply = function(target) {
const result = target.result();
this.subject().clearResult();
result.clear();
result.used = this.testApply(target);
result.missed = result.used && Math.random() >= this.itemHit(target);
result.evaded = !result.missed && Math.random() < this.itemEva(target);
result.physical = this.isPhysical();
result.drain = this.isDrain();
if (result.isHit()) {
if (this.item().damage.type > 0) {
result.critical = Math.random() < this.itemCri(target);
const value = this.makeDamageValue(target, result.critical);
this.executeDamage(target, value);
}
for (const effect of this.item().effects) {
this.applyItemEffect(target, effect);
if (effect?.code === Game_Action.EFFECT_LEARN_SKILL) {
const item = $dataItems[$gameTemp.lastActionData(1)];
const skill = $dataSkills[effect.dataId];
SceneManager.push(Scene_Map);
$gameMessage.add(`${target.name()} has learned \\I[${skill.iconIndex}]${skill.name}`);
}
}
this.applyItemUserEffect(target);
}
this.updateLastTarget(target);
};

It DOES overwrite the code for this section so may conflict with other plugins. It's probably not as good as it could be, but it works perfect for my project, and I thought I would share it.

Thanks again @Arthran for the assist!

Original thread: https://forums.rpgmakerweb.com/threads/trying-to-write-a-common-event-to-play-a-message-when-learning-a-skill.149055/