Chance to inflict state on crit

Hi there, I have made an state with yanfly buff that makes the hero inflict an state when it deals a critical hit, this is the code:

Code:
<Custom Establish Effect>
if (target.result().critical) {
target.addState(x);
} 
</Custom Establish Effect>

It works fine but now I want to give it a chance of inflict the state, lets say for example "when the hero deals a critical hit, there is a chance of 30% to inflict poison to the enemy"

Thanks for your help
What have you tried?

Hints:
Spoiler
You'd do it just like you would in an event: add another condition that checks if a random number is <=30.

Searching how to combine conditions in javascript (i.e. logical operators) and how to get a random number will take you 30s tops.
I'd write it out for you, but I think that if you take the 5 minutes to search for the js yourself you'll be better off in the long run, because this is a thing you'll be using a lot.
I use this for instant kill probability, so you are free to use this in your project:
I already made it ready for you
Code:
<Custom Establish Effect>
    var x = Math.floor(Math.random()*100)+1;
    var c = Math.min(Math.max(1, 30), 100);
    if (x <= c && target.result().critical){      
        target.addState(x);
    }
</Custom Establish Effect>
What have you tried?

Hints:
Spoiler
You'd do it just like you would in an event: add another condition that checks if a random number is <=30.

Searching how to combine conditions in javascript (i.e. logical operators) and how to get a random number will take you 30s tops.
I'd write it out for you, but I think that if you take the 5 minutes to search for the js yourself you'll be better off in the long run, because this is a thing you'll be using a lot.

Well I dont really know js but I kinda guess how to do it, generate a random number from 0 - 100 and then check if it is 30 or more, but I dont know what code is
I use this for instant kill probability, so you are free to use this in your project:
I already made it ready for you
Code:
<Custom Establish Effect>
    var x = Math.floor(Math.random()*100)+1;
    var c = Math.min(Math.max(1, 30), 100);
    if (x <= c && target.result().critical){     
        target.addState(x);
    }
</Custom Establish Effect>

Thanks for this
You are almost right, but I see u get my code, but in this particular case and any other x% chance is to check number if is equal lower or equal
desired value from 1 - 100. I do this in JS for RMMV and I've done that way in LUA scripts when I had own private server metin2 to calculate
there game mechanics and effects. (Ahh good old days)

PS glad I could help 🙂

Original thread: https://forums.rpgmakerweb.com/threads/chance-to-inflict-state-on-crit.116013/