Hi I am using Godot to create a sound based application. I want it to play the music when I press the A button on the controller or the Enter button for the first time and I want it to stop the music when I press Enter a second time. I tried everything as script but nothing works. It tells me that ''else'' is a syntax error when I don't understand why and the application refuses to launch.

At a glance, it looks like you're just missing the colon after else.

In addition to the missing colon, the lines immediately before and after the "else" should be indented.

    DaveTheCoder I don't really understand what you want to tell me so here is the script I tried to interpret. Even if I add a colon after ''else'' it considers that it still has a syntax error.

    extends AudioStreamPlayer2D

    Declare member variables here. Examples:

    var a = 2

    var b = "text"

    Called when the node enters the scene tree for the first time.

    func_process(delta):
    if Input.is_action_just_pressed("ui_accept"):
    music_position = $AudioStreamPlayer2D.get_playback_position()
    $AudioStreamPlayer2D.play

    var music_position = 0
    else:
    $AudioStreamPlayer2D.stop(music_position)

    So, if I'm understanding your intentions correctly, I think this script will do what you're looking for:

    extends AudioStreamPlayer2D
    
    
    # Variables you want to use across multiple functions should be declared above the rest of the script
    var music_position = 0.0        # Since the music position is a float, I'm guessing it starts at 0.0
    var music_isPlaying = false     # This boolean flag can tell us if the music is currently playing
    
    func _process(delta):
        if Input.is_action_just_pressed("ui_accept"):
            if !music_isPlaying:                            # If the music is not already playing
                $AudioStreamPlayer2D.play(music_position)   # Play from the currently saved position
                music_isPlaying = true                      # Set the flag to tell us the music is playing
            else:                                           # Otherwise (if music is already playing)
                music_position = $AudioStreamPlayer2D.get_playback_position()   # Record the current position
                $AudioStreamPlayer2D.stop()                                     # Stop the music
                music_isPlaying = false                     # Clear the flag to tell us music is no longer playing

    I've included comments to describe what each line is for. Let me know if any of that needs clarification.

    EDIT: Upon further reading, you could skip the manually-declared music_isPlaying variable and use the built-in $AudioStreamPlayer2D.is_playing() function instead.

      trevbot The code seems to work however as soon as I launch the application and press enter (''ui_accept'') it opens the scripts window on line 12 and the window on the right tells me that the modifications may be be lost.

      That error message (... base 'null instance' ...) means that AudioStreamPlayer2D is not a child of the node to which that script is attached.

      Inside the script attached to the AudioStreamPlayer2D, it doesn't make sense to get a reference to the node using $ or get_node(). You're already "in" the node.

      Replace:
      $AudioStreamPlayer2D.play()
      with:
      play()

      And replace:
      $AudioStreamPlayer2D.stop()
      with:
      stop()

      I missed those mistakes in my earlier reply.

        DaveTheCoder I removed all the ''$AudioStreamPlayer'' in the script and everything works. =)