# Sound isn't playing in Game Maker Studio when interacting with an object

I'm making a small game in Game Maker Studio. I have a piece of code that moves the player to the next room when he touches obj_star: this works. I have another line that tries to play sound_collect when the star is touched, but the game just moves to the next room without playing the sound. This code is in the Step event

My code is as follows:

//Get the player's input
key_right = keyboard_check(vk_right);
key_left = -keyboard_check(vk_left);
key_jump = keyboard_check_pressed(vk_space);

//React to inputs
move = key_left + key_right;
hsp = move * movespeed;
if (vsp < 10) vsp += grav;

if (place_meeting(x,y+1,obj_wall))
{
vsp = key_jump * -jumpspeed
}
//Horizontal Collision
if (place_meeting(x+hsp,y,obj_wall))
{
while(!place_meeting(x+sign(hsp),y,obj_wall))
{
x += sign(hsp);
}
hsp = 0;
}
x += hsp;

//Vertical Collision
if (place_meeting(x,y+vsp,obj_wall))
{
while(!place_meeting(x,y+sign(vsp),obj_wall))
{
y += sign(vsp);
}
vsp = 0;
}
y += vsp;

//Sounds
if (place_meeting(x+hsp,y,obj_star))
{
sound_play(sound_collect)
};
//Next Level
if (place_meeting(x+hsp,y,obj_star))
{
room_goto_next()
};

• Could you please show us the code where it is changing room? I can't seem to find it in the one you posted. And please specify what the current script is: is this a step, draw, key_press, etc.? – Alexandre Desbiens Jun 16 '15 at 19:00
• Sorry about that, edited it in. Also added the fact that it is in a step event. – newarsenic Jun 16 '15 at 19:04
• Have you tried putting a show_message just before sound_play, just to see if it really enters the if block and at least calls the sound_play function? – Alexandre Desbiens Jun 16 '15 at 19:12
• I dont know how to do that. – newarsenic Jun 16 '15 at 19:16
• Just put show_message("Test") on a new line just after sound_play, but still within the brackets. This will play the sound and show a message that you have to dismiss before the game will change room. It will let the sound the time to play if by any way it was stopped by the room_goto_next. If the sounds still doesn't play, then you have a problem with the sound file or index itself. – Alexandre Desbiens Jun 16 '15 at 19:21

I see multiple problems with your code that I can see. First is the inconsistent use of the semicolon as a terminator:

if (place_meeting(x+hsp,y,obj_star))
{
room_goto_next()
};


should be:

if (place_meeting(x+hsp,y,obj_star))
{
room_goto_next();
}


Also, because the same condition is being checked, you can combine the two if blocks to clean up the code.

if (place_meeting(x+hsp,y,obj_star))
{
sound_play(sound_collect);
room_goto_next();
}


Don't know if this will solve your problem but it will clean up your code. If it doesn't work try using the audio_sound_play function.

You should put the sounds as part of the collision event. When it collides with obj_star You could then say:

sound_play(sound_collect);

room_goto_next();