Need Help-D20 Attack Rolls-Damage Formula-Skills-Results in 0 Damage

Been reviewing and searching google for a couple days watching videos and reading the different damage formula tutorials to no avail. I have been able to get most of the formula to work but whenever I try and add the if greater than b.def then else clauses it just goes to 0.

Goal: Roll a D20 + attack-10/2+Level/2 if greater than enemies defense (ac) then roll damage 1d4 +attack-10/2 +level/2 else 0

b.def >= Math.randomInt(20)+1 ((a.atk-10)/2) + ((a.level/2)) ? (Math.max((Math.random()*3) + 1 + ((a.atk–10)/2) + (Math.floor(a.level/2)))) : 0;

tried

b.def <= (Math.max((Math.random()*19) + 1 + ((a.atk-10)/2) + (Math.floor(a.level/2)))) ? (Math.max((Math.random()*3) + 1 + ((a.atk–10)/2) + (Math.floor(a.level/2)))) : 0 ;


Separately.
Tested and works fine rolling a D4
(Math.max((Math.random()*3) + 1 + ((a.atk-10)/2) + (Math.floor(a.level/2))))

Tested and works fine rolling a D20
(Math.max((Math.random()*19) + 1 + ((a.atk-10)/2) + (Math.floor(a.level/2))))

Just need the result of the D20 to compare with the defense of the enemy and if it fails then "miss" if not activate the D4 roll.

Thanks for your help. Apologies as I would assume that is easily answerable but I feel like I had used enough time searching before posting.
Greetings!

First off, part of what you're doing doesn't belong in the damage formula. By the time you get to the damage formula, the skill has already hit, so the most you can do is make it show 0 damage (which isn't awesome from the UX perspective).

To change how you determine hits, I recommend my Custom Hit Formula plugin you can find in my plugin thread in my signature.

Next thing - as a tip to make your life easier and your formula more readable, use RPG Maker's Math.randomInt() function. It's easier to read and keep track of than using random() and multiplying each time.

Next thing - are you using this same formula on enemies and actors? By default, enemies don't have a level, so if an enemy uses this and you reference a.level, it will get undefined and zero out your entire formula.

The ultimate reason your formulae are failing is you're misusing Math.max(). I'm not sure why you're trying to use it at all, but it takes two arguments: Math.max(a, b) will return whichever number is larger, a or b.

But I don't see a reason for it to be in there at all.
Thank you for taking the time to help me out. I took all the "formulas" from various postings online. So anyways starting over. I downloaded your hit formula plug in and installed it.
This looks to be what I would use based off what was posted on the plugin link. Added the "strength" bonus and "proficiency" bonus. This alone would determine if the attack/skill in question hits.

<hitformula: Math.randomInt(20)+1+(a.atk-10)/2) + (a.level/2) >= b.def>

So adding the damage would be for a d4?

Math.randomInt(4)+1+(a.atk-10)/2) + (a.level/2)

Full formula=
<hitformula: Math.randomInt(20)+1+(a.atk-10)/2) + (a.level/2) >= b.def>Math.randomInt(4)+1+(a.atk-10)/2) + (a.level/2)

If this helps the script listed as an option in the plugin used by itself is still resulting in a 0 damage attack?
<hitformula: Math.randomInt(20)+1+a.level >= b.def>
Here are some pictures on the actor, skill and weapon I am using with the formula
Screenshot 2024-01-11 at 3.40.15 PM.png

Screenshot 2024-01-11 at 3.39.57 PM.png

Screenshot 2024-01-11 at 3.39.03 PM.png
You're still combining things.

RPG Maker works exactly like D&D:
First: Roll to see if the attack hits
Second: See how much damage the hit does

These aren't the same formula in either D&D or RPG Maker.

You had the correct thing for your hit formula, but then you tried to do weird things and add the damage into it. You're also not using it correctly - notetags go into the notes box, not the damage formula.

This goes in your skill's notes to see if it hits:
<hitformula: Math.randomInt(20)+1+(a.atk-10)/2) + (a.level/2) >= b.def>

This goes into the skill's damage formula to determine the damage:
Math.randomInt(4)+1+(a.atk-10)/2) + (a.level/2)

P.S. it doesn't make anything not work, but extra parentheses that don't change the mathematical order of operations tend to just make your formula harder to read and figure out later on.
oof, ok thank you. Missed that part about have code in the notes when reading stuff. That changes my testing drastically and should hopefully be able to figure out the rest. Although currently something is still not working by just putting those two codes into each field.

Thank you very much for getting me on the right track!
Actually, one more if you have the time. Fixed the issue with my coding with your advice so now damage is going through exactly how I want it to. I have the hit formula in notes as posted and it just always hits never misses

<hitformula: Math.randomInt(20)+1+a.level >= b.def>
This is in the notebox of the skill being used.

I did not change the plug in formula from when it was downloaded so it is still
Math.random() < this.isPhysical() ? item.successRate * 0.01 * a.hit : item.successRate * 0.01

Do I leave the plugin parameters or do I need to change that too?
Answered my own question...changed formula in parameters for universal use
Per the plugin instructions, you can put it in the parameter for universal use, but putting the hitformula tag in any individual skill overrides the global parameter.
-remove when time permits-
didn't see a section for this on the forum
There are a number of existing threads about damage formulae. (one of those is MV-specific, but you can simply change any of the code functions to their Ruby equivalents)

and didn't want to just throw a blind post out there to take up space.
That would be the correct thing to do. Your post here is what we call hijacking a thread - asking something unrelated to the original poster's question/discussion/problem (the actual topic of the thread).
Noted, and deleted. I will post this elsewhere, goodluck on yours. 👍

Original thread: https://forums.rpgmakerweb.com/threads/need-help-d20-attack-rolls-damage-formula-skills-results-in-0-damage.164705/