My script works somewhat can you help me fix it?

This script is supposed to make the player move and have boundries and on the right side my player has a boundry but on the left he just keeps going.

var MoveSpeed : float = 10;
var MaxWidth : float = 5;
function Start () {

}

function Update () 
{
	transform.position.x += Input.GetAxis("Horizontal") * MoveSpeed * Time.deltaTime;
	
	if (transform.position.x > MaxWidth)
	    transform.position.x = MaxWidth;
		
 	if (transform.position.x < -MaxWidth)
	    transform.position.x = -MaxWidth;

}

Use Mathf.Clamp when updating the transform.position.

PS: i formatted your original code.

You have a logical problem. You are not reaching the right and left limit at the same time. So, when you can’t move at one, you still can move at other.
You want to block left AND right movement when you reach the right or left limit?