user31239: My code:
const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin

bool buttonState = 0;         // variable for reading the pushbutton status
bool ledState = 0;
void setup() {
  pinMode(ledPin, OUTPUT);   // initialize the LED pin as an output:
  pinMode(buttonPin, INPUT); // initialize the pushbutton pin as an input:
}
void loop() {
  buttonState = digitalRead(buttonPin);// read the state of the pushbutton value:
  if (buttonState == HIGH) 
 {        
  ledState = !ledState;
 }
  if (ledState == HIGH) 
 {             
   digitalWrite(ledPin, HIGH); // turn LED on: 
 }
   else 
 {
    digitalWrite(ledPin, LOW); // turn LED off:
 }
}

So this is supposed to toggle a led's light on and off. currently it does do that but its not always the case. Currently, if the button is pressed on the arduino board, it has a 50-50 chance of changing its state. What part of the code is leading to this outcome?

user30002: If you're connecting the button from pin 2 to ground and not using an external pull-up, it can lead to trouble. Either use an external pull-up or set the internal one:
pinMode(buttonPin, INPUT_PULLUP); // initialize the pushbutton pin as an input:

Also, you need to debounce the button.

user16085: In addition to the debouncing suggested by the other answers, you have a further problem: your loop() will whiz through in a very short time, and run again while your finger is still on the button, turning the light off.  And then run again, turning it back on again, and so on.
You need to wait for the button to be released before registering another change.
const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin

bool ledState = 0;

void setup() {
  pinMode(ledPin, OUTPUT);   // initialize the LED pin as an output:
  pinMode(buttonPin, INPUT); // initialize the pushbutton pin as an input:
}

void loop() {
  bool buttonState = digitalRead(buttonPin);// read the state of the pushbutton value:
  if (buttonState == HIGH) 
  {        
    // Poor-man's debounce. There are much better ways of doing this!
    delay(20);

    ledState = !ledState;
    if (ledState == HIGH) 
    {             
      digitalWrite(ledPin, HIGH); // turn LED on: 
    }
    else 
    {
      digitalWrite(ledPin, LOW); // turn LED off:
    }

    // Wait for the button to be released
    while (digitalRead(buttonPin)) {}

    // Poor-man's debounce. There are much better ways of doing this!
    delay(20);
}


31239: <p>My code:</p>

<pre><code>const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin

bool buttonState = 0;         // variable for reading the pushbutton status
bool ledState = 0;
void setup() {
  pinMode(ledPin, OUTPUT);   // initialize the LED pin as an output:
  pinMode(buttonPin, INPUT); // initialize the pushbutton pin as an input:
}
void loop() {
  buttonState = digitalRead(buttonPin);// read the state of the pushbutton value:
  if (buttonState == HIGH) 
 {        
  ledState = !ledState;
 }
  if (ledState == HIGH) 
 {             
   digitalWrite(ledPin, HIGH); // turn LED on: 
 }
   else 
 {
    digitalWrite(ledPin, LOW); // turn LED off:
 }
}
</code></pre>

<p>So this is supposed to toggle a led's light on and off. currently it does do that but its not always the case. Currently, if the button is pressed on the arduino board, it has a 50-50 chance of changing its state. What part of the code is leading to this outcome?</p>
 30002: <p>If you're connecting the button from pin 2 to ground and not using an external pull-up, it can lead to trouble. Either use an external pull-up or set the internal one:</p>

<pre><code>pinMode(buttonPin, INPUT_PULLUP); // initialize the pushbutton pin as an input:
</code></pre>

<p>Also, you need to <a href="http://www.ganssle.com/debouncing.htm" rel="nofollow noreferrer">debounce the button</a>.</p>
 16085: <p>In addition to the debouncing suggested by the other answers, you have a further problem: your <code>loop()</code> will whiz through in a very short time, and run again while your finger is still on the button, turning the light off.  And then run again, turning it back on again, and so on.</p>

<p>You need to wait for the button to be released before registering another change.</p>

<pre><code>const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin

bool ledState = 0;

void setup() {
  pinMode(ledPin, OUTPUT);   // initialize the LED pin as an output:
  pinMode(buttonPin, INPUT); // initialize the pushbutton pin as an input:
}

void loop() {
  bool buttonState = digitalRead(buttonPin);// read the state of the pushbutton value:
  if (buttonState == HIGH) 
  {        
    // Poor-man's debounce. There are much better ways of doing this!
    delay(20);

    ledState = !ledState;
    if (ledState == HIGH) 
    {             
      digitalWrite(ledPin, HIGH); // turn LED on: 
    }
    else 
    {
      digitalWrite(ledPin, LOW); // turn LED off:
    }

    // Wait for the button to be released
    while (digitalRead(buttonPin)) {}

    // Poor-man's debounce. There are much better ways of doing this!
    delay(20);
}
</code></pre>
