if low SP enemy is skipping turn on rpg maker xp

Hello guys!!
So this is my problem, when the enemy dont have enough SP to use the selected skill, nothing happens and that enemy turn ends.

I have no idea how to fix that. I would like to request a script that could reroll the movement again if the selected one coudnt be done because of low SP.

- All my enemies have free movements such as basic attacks or 0 SP skills.

Please help xD

Thank you so much in advance!!
Hello guys!!
I managed to solve that problem. I will share here the script. Maybe that could be useful to someone in the future!
You can use edit and do whatever you want!
Cheers!!

Spoiler: Battle NO SP bug script
class Game_Enemy < Game_Battler
alias original_make_action make_action

def make_action
puts "[DEBUG] Inimigo esta escolhendo uma acao..."

@action = Game_BattleAction.new # Garante que @action seja sempre uma nova instancia

enemy_data = $data_enemies[@enemy_id]
return set_attack_action unless enemy_data # Se nao encontrar os dados do inimigo, ataca

# Filtro avancado: Obtem todas as acoes que podem ser usadas neste turno
valid_actions = []

enemy_data.actions.each do |action|
case action.kind
when 0 # Acoes basicas (Ataque, Defesa, Fugir, Nada)
valid_actions << [action, action.rating]
when 1 # Skill
if skill_can_use?(action.skill_id) && action_condition_met?(action)
valid_actions << [action, action.rating]
else
puts "[DEBUG] Skill #{action.skill_id} nao pode ser usada (SP insuficiente ou fora das condicoes!)"
end
end
end

# Se houver acoes validas, escolhe uma com base no rating
if valid_actions.any?
chosen_action = weighted_random_choice(valid_actions)
case chosen_action.kind
when 0 # Acoes basicas (Ataque, Defesa, Fugir, Nada)
case chosen_action.basic
when 0 # Ataque normal
puts "[DEBUG] Inimigo escolheu atacar normalmente."
set_attack_action
when 1 # Defender
puts "[DEBUG] Inimigo escolheu defender."
set_defend_action
when 2 # Fugir
puts "[DEBUG] Inimigo escolheu fugir."
set_escape_action
when 3 # Fazer nada
puts "[DEBUG] Inimigo escolheu fazer nada."
set_do_nothing_action
end
when 1 # Skill
puts "[DEBUG] Inimigo escolheu usar a skill #{chosen_action.skill_id}."
@action.kind = 1
@action.basic = 0
@action.skill_id = chosen_action.skill_id
end
else
puts "[DEBUG] Nenhuma acao valida disponivel. Forcando ataque normal."
set_attack_action
end

set_valid_target # Garante que o inimigo tenha um alvo valido
enforce_valid_action
@current_action = @action
end

# ✅ Agora os inimigos podem Defender, Fugir ou Fazer Nada!
def set_attack_action
@action.kind = 0
@action.basic = 0 # 0 = Ataque normal
@action.skill_id = nil
puts "[DEBUG] Inimigo foi configurado para ataque normal."
end

def set_defend_action
@action.kind = 0
@action.basic = 1 # 1 = Defender
@action.skill_id = nil
puts "[DEBUG] Inimigo foi configurado para defesa."
end

def set_escape_action
@action.kind = 0
@action.basic = 2 # 2 = Fugir
@action.skill_id = nil
puts "[DEBUG] Inimigo foi configurado para fuga."
end

def set_do_nothing_action
@action.kind = 0
@action.basic = 3 # 3 = Fazer nada
@action.skill_id = nil
puts "[DEBUG] Inimigo foi configurado para fazer nada."
end

# ✅ Respeita as condicoes do database (ex: turno minimo, rating)
def action_condition_met?(action)
return true if action.condition_turn_a == 0 # Se a condicao de turno for 0, pode ser usada sempre
turn_count = $game_temp.battle_turn # Obtendo o turno corretamente
return turn_count >= action.condition_turn_a && turn_count <= action.condition_turn_b
end

# ✅ Escolhe a acao de forma ponderada pelo rating
def weighted_random_choice(actions)
total_weight = actions.inject(0) { |sum, (action, rating)| sum + rating }
rnd = rand(total_weight)

actions.each do |action, rating|
return action if rnd < rating
rnd -= rating
end
end

# Metodo para garantir que @action esteja sempre correto
def enforce_valid_action
unless @action.kind == 0 || (@action.kind == 1 && skill_can_use?(@action.skill_id))
puts "[DEBUG] Acao invalida detectada! Corrigindo para ataque normal."
set_attack_action
end
puts "[DEBUG] Acao final escolhida: #{@action.inspect}"
end

# Metodo que define um alvo valido para o inimigo
def set_valid_target
enemies = $game_party.actors # Obtem todos os herois vivos
if enemies.any?
@action.target_index = rand(enemies.size) # Escolhe um alvo aleatorio
puts "[DEBUG] Inimigo definiu alvo: #{@action.target_index}"
else
puts "[DEBUG] Nenhum alvo disponivel! Acao sera ignorada."
set_do_nothing_action
end
end
end
This thread is closed due to being reported as solved. If you need it reopened, report it to let us know. Thank you!

Original thread: https://forums.rpgmakerweb.com/threads/if-low-sp-enemy-is-skipping-turn-on-rpg-maker-xp.175376/