Need help with damage formula (cause damage + add effect)

In my project, the standard setup for calculating whether a hit connects is thrown out; I use the damage formula to determine whether a hit connects.

Naturally, if I want to apply an effect on a successful hit, I can't use the built-in functionality to add an effect on an attack - they're set to auto-hit and the damage formula does the rest.

So, I have an enemy that uses a "bleed" type attack that will cause some damage and apply a damage over time effect. Here's my formula so far:

Math.randomInt(20)+a.atk > b.agi ? Math.randomInt(4)+1 + b.addState(4) : 0;
(I use agility as the defense stat)

I've highlighted in red the problematic code. The first part rolls a d20 and adds a modifier and compares this to the target's agility stat. If that works, damage is applied. However, I want it to apply the bleed state (4) as well.

Using the + symbol doesn't have the desired outcome; it just ignores the damage and decides whether to apply the state.
I've tried using no symbol, the && symbol, etc. No luck.

Any idea how I can modify this formula to do damage and apply an effect?

Thanks!
I should add that I'm not intending to add the effect on a miss (a zero, in the "else" statement) hence the need for the formula.
It should be a line break or ending statement ( ; ) f you want to add a new commands not + because using + (or &&) will try to combine the results of the two processes, so your damage formula is actually trying to combine the result of the randomInt and the addState, and such a combination will not yield a number so your damage becomes 0.

Also, your damage value should always be the last returning function because thats what the engine will use as damage. So you also need to swap your addState and Math.randomInt calls (you should add the state first, and give the damage value after).

But IDK if line breaks work with the ? operator as usually, you only use that for single effect scenarios...

I suggest using an if operator if using ; inside the ? operator doesnt work

Code:
if (Math.randomInt(20)+a.atk > b.agi) { b.addState(4);Math.randomInt(4)+1}else{0};
Wow, amazing. Yeah, that worked. The single-line formula bar for damage still kinda messes with me. Thanks a lot, Adiktuzmiko - greatly appreciated.
You still used the ? operator or shifted to if? Just curious xD
Oh, btw using the ? and : symbols doesn't appear to work in conjuction with ; as you suspected.
^ seems we posted at the same time. It did not appear to work for me. 😀 I used the code you posted, with the "if" operator.
I see.. So using ? is really only for single lines, cant use a separator inside it...

So just remember to use if, and dont forget to always have the damage value as the last thing.
will do! I think this approach will work for me going forward without any issues. Thanks again!
Well if you get issues or using the single line editor gets hard, you can always write your damage formulas as actual JS functions in a .js file and just make the damage formula call that function. xD
If you really wanted to do this with the ternary operator:

Code:
Math.randomInt(20)+a.atk > b.agi ? b.addState(4) || Math.randomInt(4) + 1 : 0;

Does it work?
--Yes
Is it readable?
--Kinda
If you looked at it 2 months from now, would you immediately know what it does?
--Probably not

Often times, writing something out as "if...else..." statements is more typing, but can help you in the long run if it's more readable and clear what is happening.
Another alternative for using a ternary operator here would be
Code:
Math.randomInt(20)+a.atk > b.agi ? (b.addState(4), Math.randomInt(4) + 1) : 0

Comparatively as (un)readable as Aloe's example, but just throwing it into the mix for information's sake.
Ahhh the Comma Operator! Completely forgot about that. It probably has a decent use case in damage formulas.

Original thread: https://forums.rpgmakerweb.com/threads/need-help-with-damage-formula-cause-damage-add-effect.108359/