I'm modifying a script - Why do I get this error?

I'm modifying CXJk's Free Movement, but my Ruby experience is very limited. I'm modifying the script to make the player pause when they turn directions, so I can add animations for turning later. The crash happens in this section:

Code:
  def interpret_move(step_left = distance_per_frame)
    @move_poll = [] if @move_poll.nil?
    if @move_poll.size > 0
      current_move = @move_poll.shift()
      d = current_move[0]
      horz = (d - 1) % 3 - 1
      vert = 1 - ((d - 1) / 3)
      turn_ok = current_move[1]
      if $wait_time <= 30
        if turn_ok
          set_direction(d)
          end
      end
              oldturncheck = $turncheck
      puts $turncheck
        
      check_event_trigger_touch_front
      processed = false
      if (d % 2 == 0 && passable?(@x, @y, d)) || (d % 2 != 0 && diagonal_passable?(@x, @y, horz, vert))
        if $wait_time == 0
          process_move(horz, vert)
          processed = true
          end
      elsif d % 2 != 0 && !diagonal_passable?(@x, @y, horz, vert)
        if passable?(@x, @y, horz + 5)
          set_direction(horz + 5) if turn_ok
          if $wait_time == 0 then process_move(horz, 0)
          processed = true
        end
        if passable?(@x, @y, 5 - vert * 3)
          set_direction(5 - vert * 3) if turn_ok
          if $wait_time == 0
            process_move(0, vert)
            processed = true
            end
        end
      end
      if(processed)
        pixelstep = CXJ::FREE_MOVEMENT::PIXELS_PER_STEP / 32.0
        if(step_left > pixelstep && !@move_poll.empty?)
          interpret_move(step_left - pixelstep)
        elsif(jumping? && !@move_poll.empty?)
          interpret_move(0)
        end
      else
        @move_poll.clear
      end
      current_move
    end
   end
  end

-on the line " if $wait_time <= 30". The crash reads:

Code:
GaryCXJk's Free Movement:968:in `interpret_move': undefined method `<=' for nil:
NilClass, NoMethodError
        from GaryCXJk's Free Movement:1488:in `interpret_move'
        from GaryCXJk's Free Movement:934:in `update'
        from Vlue's Fine Tuning Script:228:in `update'
        from Hime's Flashing Events:136:in `update'
        from Game_Player:303:in `update'
        from Victor_Engine_Step_Sound:249:in `update'
        from Scene_Map:63:in `update'
        from Hime's Item Drop Script:415:in `update'
        from AMN Combine Item w. Event:278:in `update'
        from Scene_Base:14:in `main'
        from SceneManager:23:in `run'
        from Hime's Debugging Script:10:in `block in <main>'

This only occurs if the player walks diagonally as the first move of a new game. Any idea why this happens?

Thank you in advance for any help you can give me.
It would probably help if you gave a link to the full script, as the problem may stem from something elsewhere in it.
Looks like $wait_time hasn't been defined before being used. (This is usually the problem when you see "undefined method xxx for nil" where xxx is something you were trying to do to a variable - in this comparison using <=)

The best way to make sure this won't happen is to define $wait_time ($wait_time = 0) when the game starts and/or when objects like $game_party are created upon a New Game.

I think you could also make sure it's defined right before you use it with an "or" statement (though I'm not sure I ever tried this with global variables, I assume it would work):

if ($wait_time == nil)
$wait_time = 0
end
@Kes The link to that particular script is down, but that's okay, because...

@Wavelength ...Your solution worked! I simply added another if branch checking if $wait_time != nil. Thanks so much!
No problem! Glad it worked.

Remember that (while this approach works) it's best to define it once at the beginning of the game, because otherwise you'll have to make this similar "if not nil" check everywhere you want to use it.

Original thread: https://forums.rpgmakerweb.com/threads/im-modifying-a-script-why-do-i-get-this-error.104064/