Yanfly's skill cost interfering with my damage formula?

Hello everybody!
I was trying to make a skill that uses 1/8 of your MP to transfer damage based on 1/8 of the target's MP but if you have less than 1/8 MP it will use what's left of your MP for an equivalent damage of that MP.
The MP cost gave me trouble but eventually I made it work, but right now I have trouble with the formula because when I cast it with less than 1/8 MP it does nothing. I realized that for some reason the script calculates the MP I have after already consuming the MP, so it uses 0 and that's why it does nothing.
How do I make it use what's left of my MP?




Spoiler: The script I wrote
<Custom MP Cost>

cost = Math.floor(user.mmp / 8);

if (user.mp < Math.floor(user.mmp / 8)) {

var RemainingMp = user.mp

cost = RemainingMp

}

</Custom MP Cost>



<Damage Formula>

value = target.mmp / 8

if (user.mp < Math.floor(user.mmp / 8)) {

var RemainingMpValue = user.mp / user.mmp

value = target.mmp * RemainingMpValue

}

</Damage Formula>
I think you could set a variable to the amount spent (whether it's a custom variable, like "user.mpSpent = cost", or whether it's an actual game variable), and then use that in the damage formula.
Yes I thought about it but my javascript knowledge isn't that great or any programming really so I don't know what or how to do that.
I have watched a lot of yanfly's tips and tricks and it's really helpful. I found one function that is "skill.mpCost" but when I tried putting it in it still didn't work for me.
The damage formula runs after you have already spent MP, which is why this happens. You also cannot re-call the cost function in the damage formula since it will also return 0 since your cost formula will be reevaluated and since you have 0 mp, the cost will also be 0.

To do what you want, you'd need to be able to set a global variable in the mp cost function so that you will now reference that global variable in the damage formula. I dont know though if you can make a global variable inside the cost formula, so you might need to make this variable in a JS file first, and just set it in the cost formula.
Okay I found a way to do that and it works BUT now it crashes everytime I have more than 1\8 MP right before I have less than 1\8 MP
To explain in a better way:
Let's say I have 1000 MP I use skills that cause me to get 150 MP. If I'll use the skill now, the game will crash.
I see no reason for it to crash and yet it does.
Anyways the new code is below
Spoiler: new code
<Custom MP Cost>

cost = Math.round(user.mmp / 8);

if (user.mp < Math.round(user.mmp / 8) && user.mp !== 0 &&) {

var RemainingMp = user.mp

user._skillmpCost = RemainingMp

cost = RemainingMp

}

</Custom MP Cost>



<Damage Formula>

value = target.mmp / 8

if (user.mp < Math.round(user.mmp / 8)) {

var RemainingMpValue = user._skillmpCost / user.mmp

value = target.mmp * RemainingMpValue

}

</Damage Formula>

Original thread: https://forums.rpgmakerweb.com/threads/yanflys-skill-cost-interfering-with-my-damage-formula.105709/