Audio play once when key pressed

I have a lighter and when I press F I can open my lighter and when I press G it closes. I want to play a sound when F/G is pressed. I have this script that makes my lighter open and close:

var onn : AnimationClip;
var off : AnimationClip;

function Update()
{
    if(Input.GetKeyDown(KeyCode.F))
        animation.Play("Off");
        
	if(Input.GetKeyDown(KeyCode.G))
        animation.Play("On");
}

If i make the script like this:

var onn : AnimationClip;
var off : AnimationClip;

function Update()
{
    if(Input.GetKeyDown(KeyCode.F))
        animation.Play("Off");
        audio.Play();
        
	if(Input.GetKeyDown(KeyCode.G))
        animation.Play("On");
        audio.Play();
}

or like this:

var onn : AnimationClip;
var off : AnimationClip;

function Update()
{
    if(Input.GetKeyDown(KeyCode.F))
        animation.Play("Off");
        audio.PlayOneShot;
        
	if(Input.GetKeyDown(KeyCode.G))
        animation.Play("On");
        audio.PlayOneShot;
}

It is not gonna work. Anybody has any idea how to add a sound that play everytime i press F/G ?

Either option should work, but you are missing some curly braces. You have to surround everything after an if statement with curly braces, unless you only have one line of code in the if statement.

var onn : AnimationClip;
var off : AnimationClip;
 
function Update()
{
    if(Input.GetKeyDown(KeyCode.F))
    {
        animation.Play("Off");
        audio.PlayOneShot;
    }

    if(Input.GetKeyDown(KeyCode.G))
    {
        animation.Play("On");
        audio.PlayOneShot;
    }
}