State on user, when attacking an enemy script?

I have some Pokémon in my game, Moltres being one major one and some Pokémon moves adds buffs/debuffs to the user upon hitting an enemy.

Ancient Power being one of them. That has a 10% chance of raising all stats.

I have a state called Ancient Power, and right now the state is controlled by common event upon the move. However, it only applies it to Moltres.

Well, I am now throwing in a Giant Galarian Articuno into the mix for the main villain in the Winchell World, and she too can learn Ancient Power.

I wonder if there is a script or someone could draw one up to where I could put in the skills note box something like:

<Userstate: 57, 10>

with 57 being the state ID and 10 being the percent chance the state will apply to the user when the user successfully hits an enemy.

Could I get something like this, please? Thanks.
Ruby:
# ╔═══════════════════════════════════════════════╦════════════════════╗
# ║ Title: Skills applies a user state            ║  Version: 1.00     ║
# ║ Author: Roninator2                            ║                    ║
# ╠═══════════════════════════════════════════════╬════════════════════╣
# ║ Function:                                     ║   Date Created     ║
# ║                                               ╠════════════════════╣
# ║ Add a state to the user on skill hit          ║    09 Sep 2023     ║
# ╚═══════════════════════════════════════════════╩════════════════════╝
# ╔════════════════════════════════════════════════════════════════════╗
# ║ Requires: nil                                                      ║
# ║                                                                    ║
# ╚════════════════════════════════════════════════════════════════════╝
# ╔════════════════════════════════════════════════════════════════════╗
# ║ Brief Description:                                                 ║
# ║       Allow states to be applied through skills upon hit           ║
# ╚════════════════════════════════════════════════════════════════════╝
# ╔════════════════════════════════════════════════════════════════════╗
# ║ Instructions:                                                      ║
# ║   Use <self_state: id, X> on skill that will add a state           ║
# ║   The skill used will then add the state (id) when used            ║
# ║   The X is the percentage of change that it will work.             ║
# ╚════════════════════════════════════════════════════════════════════╝
# ╔════════════════════════════════════════════════════════════════════╗
# ║ Updates:                                                           ║
# ║ 1.00 - 09 Sep 2023 - Script finished                               ║
# ║                                                                    ║
# ╚════════════════════════════════════════════════════════════════════╝
# ╔════════════════════════════════════════════════════════════════════╗
# ║ Credits and Thanks:                                                ║
# ║   Roninator2                                                       ║
# ║                                                                    ║
# ╚════════════════════════════════════════════════════════════════════╝
# ╔════════════════════════════════════════════════════════════════════╗
# ║ Terms of use:                                                      ║
# ║  Follow the original Authors terms of use where applicable         ║
# ║    - When not made by me (Roninator2)                              ║
# ║  Free for all uses in RPG Maker except nudity                      ║
# ║  Anyone using this script in their project before these terms      ║
# ║  were changed are allowed to use this script even if it conflicts  ║
# ║  with these new terms. New terms effective 03 Apr 2024             ║
# ║  No part of this code can be used with AI programs or tools        ║
# ║  Credit must be given                                              ║
# ╚════════════════════════════════════════════════════════════════════╝

module R2_Skill_State_Apply_Self
  Self_State = /<self[ _-]state:[ _-](\d+),[ -_](\d+*.\d+*)>/i
end

module DataManager
  class <<self 
    alias :load_database_original_r2_skill_state_self_apply :load_database
  end
  def self.load_database
    load_database_original_r2_skill_state_self_apply
    load_r2_skill_state_self_apply
  end
  def self.load_r2_skill_state_self_apply
    for obj in $data_skills
      next if obj.nil?
      obj.load_r2_skill_state_self_apply
    end
  end
end

class RPG::Skill
  attr_accessor :sass
  attr_accessor :sassc

  def load_r2_skill_state_self_apply
    @sass = 0 # skill apply self state
    @sassc = 0 # skill apply self state chance
    self.note.split(/[\r\n]+/).each { |line|
      case line
      when R2_Skill_State_Apply_Self::Self_State
        @sass = $1.to_i
        @sassc = $2.to_f
      end
    }
  end
end

class Game_BattlerBase
  alias :r2_pay_skill_cost_self_state_apply   :pay_skill_cost
  def pay_skill_cost(skill)
    r2_pay_skill_cost_self_state_apply(skill)
    if skill.sass != (0 || nil)
      rdnum = ((rand(1000) + 1) / 10).to_f
      self.add_state(skill.sass) if rdnum <= skill.sassc
    end
  end
end
This is not working whatsoever. No error message of any kind, just state is not being applied! I did a test battle and set the chance to 1000% but the state is not being applied to the user. I did save the project first.

Can you please fix this or otherwise help me figure out what is wrong? Thanks.
1694344367638.png


1694344420366.png
This is not working whatsoever. No error message of any kind, just state is not being applied! I did a test battle and set the chance to 1000% but the state is not being applied to the user. I did save the project first.

Can you please fix this or otherwise help me figure out what is wrong? Thanks.
View attachment 276076

View attachment 276077
Where did you put the script in your script editor?
Here:

1694347104967.png
This is not working whatsoever. No error message of any kind, just state is not being applied! I did a test battle and set the chance to 1000% but the state is not being applied to the user. I did save the project first.

Can you please fix this or otherwise help me figure out what is wrong? Thanks.
I did not expect you to have the skill cost nothing.
I'll have to find another place to put it.
I think it is not running because you have no cost to the skill.
also the regex was slightly updated. you have captured a previous version.
I did not put a cost because that was a test skill for battle test only.
I did not put a cost because that was a test skill for battle test only.
Actually looking at the code again, it should run regardless.
Unless you have something that changes it.

Check the post again, you have a previous version. I updated the script a little bit in the span of about 30 minutes after I posted it. the version you have in your script I don't have in my post.
Also 100 is the highest number for percentage. Putting 1000 is no different than 100.
I do use 1000 for my random number, but that's because I use a floating number.
Once it's divided by 10 it becomes a number between 0.0 and 100.0
Not according to the default "add state" provided with the database. It can go to 1000.

And yes 1000 is different because I have a skill and actor has that applies a state to all dragons (anything not dragon is immune to it) and it is supposed to be guaranteed. 100% has failed sometimes whereas 1000% has always worked.
Not according to the default "add state" provided with the database. It can go to 1000.

And yes 1000 is different because I have a skill and actor has that applies a state to all dragons (anything not dragon is immune to it) and it is supposed to be guaranteed. 100% has failed sometimes whereas 1000% has always worked.
The only thing I see that would affect the adding of states that much is if the state_rate is low then even a 100% could fail because if the state rate lowered the chance to below 100 then it would fail but if the chance was above 100 then when the state rate was applied it would give a result above 100.
90 * .25 or
900 * .25

But the code from what I see is using rand < chance
where rand creates a number between 0 and 1 and chance would then be between 0 and 1 as well.

The things affecting the chance are state_rate and luck. and for the attack it also adds the user attack state rate.

chance = effect.value1
chance *= state_rate(state_id)
chance *= user.atk_states_rate(state_id)
chance *= luk_effect_rate(user)


Regardless my script does not use any of this.
If the skill is used then the code calls for it to pay the skill cost.

def use_item(item)
pay_skill_cost(item) if item.is_a?(RPG::Skill)

then my script runs the build in functions of paying the skill cost and then checking for the values for that skill. and doing a random number that is adjusted for a number between 0 and 100 with one decimal place and compare it to the value you specified in the skill.
1694355328757.png
1694355350016.png
1694355378177.png

Original thread: https://forums.rpgmakerweb.com/threads/state-on-user-when-attacking-an-enemy-script.161055/