Here's the full function where the crash occurs:

Code:
action walking_guard()
{
  player = me;
	
  vec_fill(move_vec, 0); // always reset a vector to zero when creating
	
  var bag_i_counter = 0;
	
  my.JUMP_FORCE = 13;
  var gravity_var = 0;
  var gravity_force = 2;
  var jumplck = 0;
  var space_lck = 0;
  my.FEET_HEIGHT = -my.min_z+1; 
   
  my.eflags |= FAT | NARROW; // set both flags to prevent automatic 
                             //   recalculation on scale changes.
  my.emask |= ENABLE_SHOOT;
  my.event = spell_fly_damage;

  my.HEALTH = 500;
	
  // set FLAG2 to make the guard detectable by c_scan, 
  // and store the guard pointer in its own CREATOR skill 
  // so that the detecting entity knows its enemy	
  set(my,FLAG2);  
  my.CREATOR = me;
	 
  set(my, POLYGON);
  
  camera.tilt = -5;
  
  my.STATE = 1;
	 
  while(my.x)
  {
    player_health();
		
    // state 1: walking ////////////////////////////////////////////
		
    if (my.STATE == 1)
    {
      while(mouse_mode == 1)
      {
        move_vec.x = 0;
        move_vec.y = 0;
		   	
        wait(1);
      }
	   	 
      // MOVE PLAYER FORWARD/BACKWARD WITH "W" AND "S" KEYS, AND SIDEWAYS WITH 
      //   "A" AND "D" KEYS
      move_vec.x = key_w - key_s;
      move_vec.y = key_a - key_d;
		  
	
      // MOVING FORWARD/BACKWARD KEYS - OPTIONS: UP AND DOWN ARROW KEYS, OR "W" 
      //   AND "S" KEYS
			
      distance = (key_w-key_s)*20*time_step; 
      distance_crawl = (key_w-key_s) * 5 * time_step;
			
      if(key_i)
      {	
        bag_i_counter = 1;
				
        if(bag_i_counter == 1)
        {
          inv_open_bag(bag, 500, 0);
	  mouse_mode = 1;
					
	  if(key_i)
	  {
	    bag_i_counter = 0;
	    inv_close_bag(bag);
	    mouse_mode = 0;
	  }
        }
      }
			
      // JUMPING CODE
      if( (key_space)  && !space_lck && !jumplck)
      {
        gravity_var = my.JUMP_FORCE;
        jumplck = 1;
        my.ANIMATION = 0;
      }
      space_lck = key_space;
      gravity_var -= gravity_force * time_step;
	      
      // RUNNING
      vec_normalize(move_vec, 20 * time_step); // normalize the vector - 
        //  otherwise walking forward and sideways at the same time will be 
        //  faster (~1.4151x)
			
      c_move(me, move_vec, vector(0, 0, gravity_var), IGNORE_PASSABLE | GLIDE); 
			
      _handle_cam_3rdperson();
			  
      if(((key_w == 1) || (key_s == 1)) && (mouse_right == 0))
      {
        // FORWARD/BACKWARD MOVEMENT ANIMATION    
				
        my.ANIMATION += 1*distance;
				
        ent_animate(me,"run",my.ANIMATION,ANM_CYCLE);
      }
      else
      if(((key_w == 1) || (key_s == 1) || (key_a == 1) || (key_d == 1)) && 
        (mouse_right == 1))
      {
        // CRAWLING  
        vec_normalize(move_vec, 5 * time_step); // normalize the vector - 
          //  otherwise walking forward and sideways at the same time will be 
          //  faster (~1.4151x)  
				
        // FORWARD/BACKWARD MOVEMENT ANIMATION    
				
        my.ANIMATION += 1*distance_crawl;
				
        ent_animate(me,"crawl",my.ANIMATION,ANM_CYCLE);
      }
      else
      if(mouse_right == 1)
      {
        // FORWARD/BACKWARD MOVEMENT ANIMATION    
				
        my.ANIMATION += 1*distance;
				
        ent_animate(me,"crouch",my.ANIMATION,ANM_CYCLE);
      }
      else
      {
        // STANDING STILL ANIMATION
				
        my.ANIMATION += 1*time_step;
        ent_animate(me,"idle",my.ANIMATION,ANM_CYCLE); // "idle" stance 
          //  continual animation
      }
			
      // adjust entity to the ground height, using a downwards trace

      if(c_trace(my.x, vector(my.x, my.y, my.z - my.FEET_HEIGHT), IGNORE_ME | 
        IGNORE_PASSABLE | IGNORE_PASSENTS | USE_POLYGON))
      {
        my.z = target.z + (my.FEET_HEIGHT);	
        gravity_var = 0;
        jumplck = 0;	
      }	
			
      // ATTACK CODE
		
      if (mouse_left) 
      { // key pressed -> go to state 2
        my.ANIMATION = 0;
        my.STATE = 2;
      }
			
      // state 2: guard swing attacking /////////////////////////////////// 
      if (my.STATE == 2) 
      {	
        while (1)
        {
          my.ANIMATION += 15*time_step;
          ent_animate(me,"attack",my.ANIMATION,0); // "attack" one-shot   
            //  animation
     	  if (my.ANIMATION > 100) // finish the attack swing and go to state 3
          { 
            while(1)
     	    {
     	      player_ctrace();
     	
     	      my.ANIMATION -= 35*time_step;
     						
              ent_animate(me,"attack",my.ANIMATION,0); // "attack" one-shot   
                //  animation
     						
     						
     	      if (my.ANIMATION <= 0)
     	      {							
     	        my.STATE = 3;
     	        break;
     	      }
     	      wait(1);
	    }		
	  }
					
          if (my.STATE == 3)
	  {
	    my.ANIMATION = 0;
	    my.STATE = 1;
	    break;
          }
						
     	  wait (1);	
        }	
      }  
		
      // DEATH OF PLAYER CODE
		
      if (my.HEALTH <= 0)
      {
        my.ANIMATION = 0;
        while(1)
        {
          ent_animate(me,"death",my.ANIMATION,0);
	  my.ANIMATION += 5*time_step;
	
          if (my.ANIMATION >= 100)
            return;
	  wait(1);
        }
      }			
    }	
    wait(1);
  }
}