VX/VXA Script command: check state

I'm not very good with scripts. Can you tell me how to fix this check so that it works?
The script is launch through a regular command in the event.

Spoiler
Ruby:
# Check for the presence of state 46 in any character
members46 = $game_party.actors.select { |actor| actor.states.include?(46) }

$game_party.members.each do |actor|
  if actor && actor.state?(46)

# If characters with state_id 46 are found
members46.each do |member|
  # Check if the character has skill id_5
  unless member.skills.include?(5)
    # If there is no skill, add state_id 47
    member.add_state(47)
  end
end
Game_Party does not have an actors method so your first line won't work, use members instead or even all_members if you want this to apply to non-battle members too when your party is in battle.
$game_party.members.each do |actor| if actor && actor.state?(46)
You should remove this part as it does not do anything and will crash because you need to put endkeywords at the end of each block (loops like obj.each do |x| and if/unless statements that are not at the end of a line)

Also,member.skillsstores the skills objects, not the skills ids somember.skills.include?(5) is likely to not work the way you intended.

You can do this instead:
Ruby:
# Keep the actors with state id 46
members46 = $game_party.members.select { |actor| actor.state?(46) }
# Add state 47 to them unless they have the skill with id 5
members46.each { |m| m.add_state(47) unless m.skill_learn?($data_skills[5])}
Game_Party does not have an actors method so your first line won't work, use members instead or even all_members if you want this to apply to non-battle members too when your party is in battle.

You should remove this part as it does not do anything and will crash because you need to put endkeywords at the end of each block (loops like obj.each do |x| and if/unless statements that are not at the end of a line)

Also,member.skillsstores the skills objects, not the skills ids somember.skills.include?(5) is likely to not work the way you intended.

You can do this instead:
Ruby:
# Keep the actors with state id 46
members46 = $game_party.members.select { |actor| actor.state?(46) }
# Add state 47 to them unless they have the skill with id 5
members46.each { |m| m.add_state(47) unless m.skill_learn?($data_skills[5])}
Unfortunately, it didn't work 🙁

Tell me, do you know how to check the following with a script:
I would like to perform a check so that if the group leader is wearing armor "7",
then replace it with armor "6".

I also need another check:
If the group leader does not have armor "7" or armor "6", but has the status "42", then remove the status "42".
Unfortunately, it didn't work 🙁
Sorry, it was my mistake, skill_learn? checks if the skill was learned through actor level up or things like this, not all skills, here is a fixed version:

Ruby:
# Keep the actors with state id 46
members46 = $game_party.members.select { |actor| actor.state?(46)}
# Add state 47 to them unless they have the skill with id 5
members46.each { |m| m.add_state(47) unless m.skills.include?($data_skills[5])}

Tell me, do you know how to check the following with a script:
I would like to perform a check so that if the group leader is wearing armor "7",
then replace it with armor "6".
I would have told you that this can be fully performed by events but surprisingly the event command to change the equipment must have a fixed actor given.

Dealing with weapons/armors is less trivial because we have to check the slots and add/remove items from the party's inventory, anyway here is something:
Ruby:
#store the leader in a variable for easier access
l = $game_party.leader
#get the slot of the armor with id 7
if l #just making sure the party is not empty
  slot_id = l.equips.index($data_armors[7])
  #if a slot was found with said armor, replace it:
  if slot_id
    #add the item to the inventory
    $game_party.gain_item($data_armors[6], 1)
    #exchange it with the worn item (does not work if the other item is not in inventory!)
    l.change_equip_by_id(slot_id,6)
    #remove the previous item from inventory
    $game_party.lose_item($data_armors[7], 1)
  end #end of if statement
end #end of if statement

I also need another check:
If the group leader does not have armor "7" or armor "6", but has the status "42", then remove the status "42".
This one can fully be done with event commands with ease, here is the script version but I suggest you try to do this with events if you don't understand scripting.

Ruby:
l = $game_party.leader
#if leader has state 47 and wears armor 6 or 7, then remove state 47:
if l && l.state?(47) && [6,7].any? {|i| l.armors.include?($data_armors[i])}
  l.remove_state(47)
end #end of if statement

I strongly suggest you try to do most things with event commands whenever possible, also you should check this thread about turning event commands into script calls.

Original thread: https://forums.rpgmakerweb.com/threads/script-command-check-state.179516/