Im not sure what your looking for, cause what I think you want doesnt match your sample code.
Try looking at this and see if it makes sense, and it the effect you are after.
var this_sound;
while(1) //the existing while loop you mentioned
{
...
if(player_proxb==1) //if near player (?)
{
if(snd_playing(this_sound)==0) //AND sound not already running
{
this_sound = snd_play(gear_wav, 100, 0); //remember snd_play DOESNT self-repeat
}
}
if(snd_playing(this_sound)==0)
{
snd_stop(this_sound); //stop it "properly" when its finished itself
}
...
wait(1);
}