How to get button to keep servo motor from staying in position

hello! so I am having a little bit of trouble, I am trying to get a button to stay on so my servo motor can stay in a position until the button is pressed again. Furthermore, I am using the servo motor to move a rack and pinion. This is all the code that I have right now

#include <Servo.h>

const int buttonpin = 2;
const int servopin = 3; 
Servo servo;
int button;


void setup()
{
  pinMode(buttonpin, INPUT);
  servo.attach(servopin);
}

void loop()
{
  button = digitalRead(buttonpin);
      if (button == HIGH) {
    servo.write(180);
    delay(10);
      }
  else {
    servo.write(0);
    delay(10);
  }
}

I think what you need is state detection. In other words, the code continuously monitors the button to see if it has been pressed, and each time it is pressed it changes the state (of the servo). So you press it once, the servo moves to 180. Until its pressed again, it stays at 180 because that is the stored state. This example is slightly more complex than what you need, but it will give you the idea.

Use a variable to "remember" whether the button was pressed, and change its state every time you push the button.

Buttons bounce, so you may need a button bounce library to get good state changes.

You need to detect when the button becomes pressed rather than when it is pressed

Tale a look at the StateChangeDetection example in the IDE

hey so i tried the code and it worked, only issue is when I first start up my code, I have to click the button once before it goes to pos= 0

So, you know which line of code causes the servo to move to position 0. What do you suppose would happen if you copied that line to setup(), before you attach the servo?

What DID actually happen when you tried that?