I have my main function that sets up the selected level, starts playing a background song if it's not already playing, and allows the user to press keys to mute the song, and toggle panels.

For some reason, When I start it up, the main function runs once and everything runs fine, but when I get the second level, the main function is run again, but the keys do not work but when I get to the third level, everything works fine again. It only works every other iteration.

Here is the main function. [yes it's sloppy and not optimized]
Code:
void main(){
	var timerShow = 0;
	// preload in buffer all created models:
	preload_mode = 3;
	// show all errors:
	warn_level = 6;
	// set maximal framerate:
	fps_max = 60;

	// set video parameters:
	//video_mode =12;
	//video_screen = 1;

	video_set(sys_metrics(0), sys_metrics(1), 0, 1);
	//video_set(1366, 768, 0, 1); 
	video_aspect = 1.777;
	video_window(0, 0, 0, "Untitled");
	mouse_map = arrow;
	mouse_mode = 4;
//	level_load(NULL);
	text_outline = 100;
	
	choose->pos_x = (sys_metrics(0) - choose.size_x)/2;
	choose->pos_y = (sys_metrics(1) - choose.size_y)/2;
	blackPan->size_x = sys_metrics(0);
	blackPan->size_y = sys_metrics(1);
	timer_pan->pos_y = sys_metrics(1) - 68;
	//collectPan->size_x = bmap_width("way.pcx") * 2;
	//collectPan->size_y = bmap_height("way.pcx");
	collectPan->pos_x = sys_metrics(0) - 276;
	collectPan->pos_y = sys_metrics(1) -68;
	
	
	
	if(started == 0)
		initalLaunch();
		
	wait_for(initalLaunch);
	
	//set(timer_pan, SHOW);
	mouse_mode = 0;
	// run physX:
	physX_open();
	// freeze the game:
	freeze_mode = 1;
	// load the level:
	level_load(levelStr);
	// wait three frames:
	wait(3);
	// unfreeze the game:
	freeze_mode = 0;
	// wait till player exist:
	heroEnt = ent_create(ball_string, vector(0,0,100), actBall);
	while(!heroEnt){ wait(1); }
	
	while(1){
		
		if(media_playing(background) == 0)
		{
			
			background = media_play("Prince Yeti - coco.mp3", NULL, volume);
		}
		
		if(key_pressed(key_for_str("m")) == 1)
		{
			while(key_pressed(key_for_str("m")) == 1){wait(1);}
			if(volume == 50)
				volume = 1;
			else
				volume = 50;
			media_tune(background, volume,0, 0);
		}
		
		if(key_pressed(key_for_str("t")) == 1)
		{
			while(key_pressed(key_for_str("t")) == 1){wait(1);}
			toggle(timer_pan, SHOW);
		}
		
		if(initial == 0)
		{
			minutes = 0;
			seconds = 0;
			milliseconds = 0;
			initial = 1;
		}
		if(collectChange == 1)
		{
			str_cpy(collectString, "");
			str_cat_num(collectString, "%.0f", collected);
			str_cat(collectString, "/");
			str_cat_num(collectString, "%.0f", needToCollect);
			
			FONT* newFont = font_create("Calibri#40");
			pan_setstring(collectPan, 0, 55, collectPos , newFont, collectString);
			collectChange = 0;
		}
		
		
		// cycle throw all entities:
		for(you = ent_next(NULL); you; you = ent_next(you)){
			// if transparent object, was found:
			if(you.objType == objTrans){
				// check whether we are between camera and player:
				if((you.x + you.min_x < maxv(camera.x, heroEnt.x) + 5 && you.x + you.max_x > minv(camera.x, heroEnt.x) - 5)
				&& (you.y + you.min_y < maxv(camera.y, heroEnt.y) + 5 && you.y + you.max_y > minv(camera.y, heroEnt.y) - 5)){
					// decrease alpha:
					you.alpha = clamp(you.alpha - 50 * time_step, 20, 100);
				}
				else{
					// increase alpha:
					you.alpha = clamp(you.alpha + 50 * time_step, 20, 100);
				}			
			}
		}
		// wait one frame:
		wait(1);
	}
}