Skill Cancel & After Using Skill SkillId

I find out $gameActors.actor(actorId)._actions[0]._ item._itemId is a Sellected Skill Id in thread. And this is 0 when I start battle.
But this value does not change when I cancel the skill.
And when I use skill the value does not defined because of 'TypeError: Cannot read property '_item' of undefined' before next turn start. (I find those with SRDude's HUD MAKER and there are only one error 'TypeError: Cannot read property '_item' of undefined')
I want the value to be 0 again when canceled and used skill.
Please Help Me!😢

This is my code.
Cost ID = {};

Cost.SID = function (skill) {
var caster = $gameActors.actor($gameParty.members()[0].actorId());
var action = caster._actions[0]._item;
var sid = action._itemId
{return sid}
};

+ I am sorry for the mistake of using English. But if you write something for me. I'll try hard to use that!
You gave us your function, but completely forgot to give us one more important thing.... How you use that function. If we don't know how you use it, it is not the easiest to fix it.
You gave us your function, but completely forgot to give us one more important thing.... How you use that function. If we don't know how you use it, it is not the easiest to fix it.
I write notetags <cost : number> in skills for use $dataSkills[skillId].meta.cost(let skillcost).
And I make other cost that partymembers share(let partycost).
If partycost minus sum of skillcost is more than Game_BattlerBase.prototype.skillMpCost(skill) they can't pay skill cost, so they can't use skill.
At first time I think it works for me. But after attack error ocuurs.
And the skillcost doesn't cancel even if I cancel skill...
So the Game_BattlerBase stuff calls your Cost.SID?
So the Game_BattlerBase stuff calls your Cost.SID?

Cost.SID just mean skiiId.
To define skillcost, I need skillid so I make Cost.SID per actors.
I think I don't have to make that...
And also I use Game_Battler.prototype.canpayskillcost (skill) and change skill's mpcost to cost (just that value is equal with cost)
I can't show you all of the function now because I am using phone. I'll show you after change SID.
Ok, I'll wait until then. Because as it is now, it is impossible to orientate in.
This is my all script.
It works like last time.
And I find out error when I use skill menu.
'TypeError: Cannot read property '_item' of undefined'
I think I should add $gameparty,inbattle()


var COST = {};



COST.party1 = function cost1() {

var c1 = null;

if ($gameParty.members()[0].actorId() > 0) {
c1 = 5 + Math.trunc($gameActors.actor($gameParty.members()[0].actorId()).level / 5)
} else {c1 = 0};
{ return c1 }
};

COST.party2 = function cost2() {

var c2 = null;

if ($gameParty.members().length >= 2) {
c2 = 5 + Math.trunc($gameActors.actor($gameParty.members()[1].actorId()).level / 5)
} else {c2 = 0};
{ return c2 }
};

COST.party3 = function cost3() {

var c3 = null;

if ($gameParty.members().length >= 3) {
c3 = 5 + Math.trunc($gameActors.actor($gameParty.members()[2].actorId()).level / 5)
} else {c3 = 0};
{ return c3 }
};

COST.party4 = function cost4() {

var c4 = null;

if ($gameParty.members().length >= 4) {
c4 = 5 + Math.trunc($gameActors.actor($gameParty.members()[3].actorId()).level / 5)
} else {c4 = 0};
{ return c4 }
};

COST.partyvalue = function cost() {

var C = COST.party1() + COST.party2() + COST.party3() + COST.party4();

{return C}
};


COST.Skill1 = function scost1(skill){

var sid = 0;
var caster = $gameActors.actor($gameParty.members()[0].actorId());
var action = caster._actions[0]._item

if (action.isSkill()) {
if (action._itemId > 0) {var sid = $dataSkills[action._itemId].meta.cost;}
else {var sid = 0;} }
else {var sid = 0;}

{return sid}

};

COST.Skill2 = function scost2(skill){

var sid = 0;
if ($gameParty.members().length >= 2){
var caster = $gameActors.actor($gameParty.members()[1].actorId());
var action = caster._actions[0]._item;

if (action.isSkill()) {
if (action._itemId > 0) {var sid = $dataSkills[action._itemId].meta.cost;}
else {var sid = 0;} }
else {var sid = 0;} }
else {var sid = 0;}


{return sid}

};

COST.Skill3 = function scost3(skill){

var sid = 0;
if ($gameParty.members().length >= 3){
var caster = $gameActors.actor($gameParty.members()[2].actorId());
var action = caster._actions[0]._item;

if (action.isSkill()) {
if (action._itemId > 0) {var sid = $dataSkills[action._itemId].meta.cost;}
else {var sid = 0;} }
else {var sid = 0;} }
else {var sid = 0;}



{return sid}

};



COST.TSkill = function totalscost(skill){



var total = Number(COST.Skill1(skill)) + Number(COST.Skill2(skill)) + Number(COST.Skill3(skill));


{return total}


};


Game_BattlerBase.prototype.canPaySkillCost = function(skill) {
if (Game_Actor.prototype.isActor() == true) {return this._tp >= this.skillTpCost(skill) && this._mp >= this.skillMpCost(skill) && COST.partyvalue() - COST.TSkill(skill) >= this.skillMpCost(skill);
} else {return this._tp >= this.skillTpCost(skill) && this._mp >= this.skillMpCost(skill)};
};
The code you sent me is very unclean, but apart from bad writing there is nothing wrong with it, no mistake whatsoever. I polished it a little bit by deleting some unnecessary things (not all of them though, because I want you to be able to recognize the code.
But once again, it is important where and how this code is called. Which functions call them, which plugin commands call them, or whatever is supposed to call them. And that is what I still don't know, because this code does not contain information as to what triggers it.

Here is the a bit polished code btw.
Code:
var COST = {};

COST.party1 = function cost1() {
    var c1; // deleted "= null", because it is unnecessary
    if ($gameParty.members()[0].actorId() > 0) {
    c1 = 5 + Math.trunc($gameActors.actor($gameParty.members()[0].actorId()).level / 5)
    } else {c1 = 0};
    return c1 // deleted curly brackets
};
    
COST.party2 = function cost2() {
    var c2;
    if ($gameParty.members().length >= 2) {
    c2 = 5 + Math.trunc($gameActors.actor($gameParty.members()[1].actorId()).level / 5)
    } else {c2 = 0};
    return c2
};

COST.party3 = function cost3() {
    var c3;
    if ($gameParty.members().length >= 3) {
    c3 = 5 + Math.trunc($gameActors.actor($gameParty.members()[2].actorId()).level / 5)
    } else {c3 = 0};
    return c3
};
    
COST.party4 = function cost4() {
    var c4;
    if ($gameParty.members().length >= 4) {
    c4 = 5 + Math.trunc($gameActors.actor($gameParty.members()[3].actorId()).level / 5)
    } else {c4 = 0};
    return c4
};

COST.partyvalue = function cost() {
var C = COST.party1() + COST.party2() + COST.party3() + COST.party4();
return C
};


COST.Skill1 = function scost1(skill){ // parameter is absolutely unnecessary, because this function does not use this parameter
var sid = 0;
var caster = $gameActors.actor($gameParty.members()[0].actorId());
var action = caster._actions[0]._item
if (action.isSkill()) {
if (action._itemId > 0) {sid = $dataSkills[action._itemId].meta.cost;} // you had a couple of unnecessary var sids there, but sid is already defined. Which makes the sid = 0 after "else" completely redundant btw and can be deleted too.
else {sid = 0;} }
else {sid = 0;}
return sid
};

COST.Skill2 = function scost2(skill){ // parameter is absolutely unnecessary, because this function does not use this parameter
var sid = 0;
if ($gameParty.members().length >= 2){
var caster = $gameActors.actor($gameParty.members()[1].actorId());
var action = caster._actions[0]._item;
if (action.isSkill()) {
if (action._itemId > 0) {sid = $dataSkills[action._itemId].meta.cost;}
else {sid = 0;} }
else {sid = 0;} }
else {sid = 0;}
return sid
};

COST.Skill3 = function scost3(skill){ // parameter is absolutely unnecessary, because this function does not use this parameter
var sid = 0;
if ($gameParty.members().length >= 3){
var caster = $gameActors.actor($gameParty.members()[2].actorId());
var action = caster._actions[0]._item;
if (action.isSkill()) {
if (action._itemId > 0) {sid = $dataSkills[action._itemId].meta.cost;}
else {sid = 0;} }
else {sid = 0;} }
else {sid = 0;}
return sid

};

COST.TSkill = function totalscost(skill){
var total = Number(COST.Skill1(skill)) + Number(COST.Skill2(skill)) + Number(COST.Skill3(skill));
return total
};


Game_BattlerBase.prototype.canPaySkillCost = function(skill) {
if (Game_Actor.prototype.isActor() == true)
{return this._tp >= this.skillTpCost(skill) && this._mp >= this.skillMpCost(skill) && COST.partyvalue() - COST.TSkill(skill) >= this.skillMpCost(skill);
} else {return this._tp >= this.skillTpCost(skill) && this._mp >= this.skillMpCost(skill)};
};

Original thread: https://forums.rpgmakerweb.com/threads/skill-cancel-after-using-skill-skillid.86659/