Why does my character not detach from the rope

I have a rope swing and have managed to get the player to attach to the rope using a parent system, but I want it so that if the player presses a button then the character will jump off and detach from the rope. I have done Debugs and it says that the button is not being pressed when on the ground which is good but then as soon as he touches the rope the button is pressed which is not?
Thanks.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RopeAttach : MonoBehaviour
{
    public GameObject player;
    public Vector3 pos;
    public Rigidbody PlayerRB;
    private Collider rope_collider;
    private bool APress = false;
    private bool OnRope = false;


    void Start()
    {
        
        player = GameObject.Find("player");
        PlayerRB = player.GetComponent<Rigidbody>();
        rope_collider = GetComponent<Collider>();

    }

    void Update ()
    {
        if (Input.GetKeyDown(KeyCode.Joystick1Button0))
        {
            APress = true;
        }
        Debug.Log(APress);
    }


    private void OnTriggerEnter(Collider other)
    {
        if (other.gameObject == player)
        {
            OnRope = true;
            player.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezePositionY | RigidbodyConstraints.FreezePositionZ;
            PlayerRB.useGravity = false;
            player.transform.parent = transform;
            


           
        }
    }

    void RopeExit()
    {
        if (APress == true && OnRope == true)
        {

            rope_collider.enabled = false;
            player.transform.parent = null;
            player.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.None;
            player.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezePositionZ | RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationY | RigidbodyConstraints.FreezeRotationZ;
            PlayerRB.useGravity = true;
            player.transform.parent = null;
            APress = false;
            OnRope = false;
        }
    }

    
}

As soon as the Update method catches a button press, it sets APress to true and holds that value from this point onwards. When RopeExit() fires, it will always find APress == true if the button was pressed at anypoint beforehand. I assume this is your problem even though you don’t show us where RopeExit() is being called.

You can add an else branch to change APress to false or simply add the check for button press in the if statement inside the RopeExit() function.

         if (Input.GetKeyDown(KeyCode.Joystick1Button0))
         {
             APress = true;
         } else {
              APress = false;
         }

or

 void RopeExit()
 {
     if (Input.GetKeyDown(KeyCode.Joystick1Button0) == true && OnRope == true)
     {
     //rest of code
    //(this actually requires that RopeExit() is called every frame, because GetKeyDown will only be true on the frame where the button was pressed)