Damage Formula - Adding and Removing States

After reading several tutorials on formulas, I've written quite a few on my own, however, with the latest one, I stumbled upon a weird problem. I really don't know why it doesn't work (mostly due to my lack of sleep over the last days I suppose), but this here is my setup for a skill, that first gives you a random elemental buff (the strongest of them with a lesser chance), then removes all others of the same kind, while dealing damage to yourself.

My code looks like this:

#Blade Infusal. Choose one of 4 random stats, thunder is the rarest. def blade_infusal(a, c = 1 + rand(100) if c >= 0 && c <= 29 #Add Element, Remove similar ones, Deal damage to self a.add_state(37); a.remove_state(38); a.remove_state(39); a.remove_state(40); a.hp * 0.1 elsif c >= 30 && c <= 59 #Add Element, Remove similar ones, Deal damage to self a.add_state(38); a.remove_state(37); a.remove_state(39); a.remove_state(40); a.hp * 0.1 elsif c >= 60 && c <= 89 #Add Element, Remove similar ones, Deal damage to self a.add_state(39); a.remove_state(38); a.remove_state(37); a.remove_state(40); a.hp * 0.1 elsif c >= 90 #Add Element, Remove similar ones, Deal damage to self a.add_state(40); a.remove_state(38); a.remove_state(39); a.remove_state(37); a.hp * 0.1 end endWhat's happening is that I do get a random stat, and I do get the intended damage to myself, however, the states already present are not being removed and I can basically stack something like "Water Blade" with "Fire Blade" which is pretty nonsensical.

Terribly sorry if I sound rude, I'm just a little bit tired. It would be amazing if somebody could help me out here! (It's probably something obvious, isn't it.. D🙂

While I'm at it, what would that even do? If I have two states on that both affect the element of physical attacks, which one is used for calculations? The one with higher priority?

(I know this is a bit of scripting, but I guess it's mostly formula, as it's the same use?! If I should have rather posted this in script support, I'm sorry, my bad.)
When I test it, the states are being removed, so I'm not sure what's wrong in your case. Are you using any other scripts?

Also, to answer your question, if you have two elements included in your physical attack, it'll use the one that has the most beneficial effect for the user (for attacks, that's whatever does the most damage to the enemy. For support/heals, it's whatever gives you the most healing)
As far as I can tell, your formula looks fine.
 
The only thing I'd do is rewrite the code a bit.

#Blade Infusal. Choose one of 4 random stats, thunder is the rarest.
 

def blade_infusal(a, 😎

x = 37

while x < 41
   a.remove_state(x)

   x += 1

end

   c = 1 + rand(100)
   if c >= 0 && c <= 29
     a.add_state(37)
   elsif c >= 30 && c <= 59
     a.add_state(38)
   elsif c >= 60 && c <= 89
     a.add_state(39)
   elsif c >= 90
     a.add_state(40)
   end

   a.hp * 0.1
 end

Maybe putting all the remove states in one place and doing it first will clear up the issue.

Maybe...
@Maliki79 
I tried yours, but if I do, it doesn't seem to do anything at all. It just puts "0" damage to the User and doesn't add any states. It does seem more practical though! ^-^

@Trihan

Ah, I see! Thanks for the answer first of all, the code does seem to work today... I swear it didn't yesterday ._. Thanks  ^-^

But good that I opened up the topic anyway, for one more formula related issue occured, plus another little question.

In the original version of Astralon, I had a skill that dealt damage to an enemy, then raised ATK or MAT, depending on which of those is higher in value.

My code looks like that: 

a.atk >= a.mat ? a.add_buff(atk,3) : a.add_buff(mat,3); 0.8 * a.atkAnd again, I don't seem to notice any changes in the game. Did I screw up with the code, or am I being stupid again? (Possibly both?)

Second little thing, I couldn't seem to find just how much a default "buff" or "debuff" in RPG Maker actually does in value. Is it +50%? 100%? Would be good to know, 'cause I might be working more with states if those are too little/high.
@Trihan

Ah, I see! Thanks for the answer first of all, the code does seem to work today... I swear it didn't yesterday ._. Thanks  ^-^
If you're using the troops tab's battle test function, make sure you save the project beforehand. It's possible you launched the game in a state prior to when your formula worked.
class Game_Battler < Game_BattlerBase  def blade_infusal(a,    c = 1 + rand(100)    a.remove_state(37)    a.remove_state(38)    a.remove_state(39)    a.remove_state(40)    case c    when 1..29      a.add_state(37)    when 30..59      a.add_state(38)    when 60..89      a.add_state(39)    else      a.add_state(40)    end    return a.hp * 0.1  end end
Code:
(a.atk >= a.mat ? a.add_buff(2,3) : a.add_buff(4,3)); 0.8 * a.atk
havent tested either of them
The second one works like a charm, the first one doesn't quite do the trick. It does add one of the states initially, however, upon casting the spell again, it only removes the old one, it doesn't add a new one, which is counter productive, haha 🙂

Also, @dungeon diver, this was indeed what caused that first problem. I was so used to editing formulas in the database only, where simply clicking apply sufficed, that I didn't even think about that. Gosh I feel stupid now.

Also, does anyone know the Buff Rate? It'd be interesting to know! 🙂 
It's 25%.
Ah, that's reasonable. Thanks a bunch! I guess everything's solved here! Thanks everybody! 🙂

Original thread: https://forums.rpgmakerweb.com/threads/damage-formula-adding-and-removing-states.39994/