How to return an object back to it's original position when the player steps off it?

I'm trying to get a platform to move to a certain point when the player steps on it and then have it move back when the player steps off. I've gotten quite close but frustration overwhelmed me. Thank you for any help.

It's very messy so I apologize for that. I was testing quite a bit.

var speed : Vector3 = Vector3(0, 0, 2);
//var initPos : Vector3;

private var onPlat : boolean = false;

var endPoint : Vector3;

var duration : float = 2;

var startPoint : Vector3;

//private var startTime : float;

function Start(){
    //startPoint = transform.position;
    //startTime = Time.deltaTime;
}

function FixedUpdate(){
    if (onPlat)
        rigidbody.MovePosition(Vector3.Lerp(startPoint, endPoint, 
                                            (Time.time * duration)));
   else rigidbody.MovePosition(Vector3.Lerp(transform.position, startPoint,
                                            (Time.time * duration)));
}

function OnCollisionEnter(hit : Collision) {
    if (hit.gameObject.tag == "Player") {
        onPlat = true;
        //rigidbody.MovePosition(rigidbody.position + speed * Time.deltaTime);
        //transform.position = Vector3.Lerp(startPoint, endPoint,
        //                                  (Time.time+startTime) / duration);
    }
    else onPlat = false;
}

/*function OnCollisionExit() {
    onPlat = false;
    //transform.position = Vector3.Lerp(startPoint, endPoint,
    //                                  (Time.time-startTime) / duration);
}*/

/*function MoveBack() {
    if(!onPlat) {
        transform.position = Vector3.Lerp(startPoint, endPoint,
                                          (Time.time-startTime) / duration);
        //rigidbody.MovePosition(rigidbody.position - speed * Time.deltaTime);
        //transform.position.z = initPos.z + 
        //    Mathf.Clamp01(Time.time * platformSpeed, transform.position.z);
    }
}

function OnCollisionExit() {
    if(!onPlat) {
        rigidbody.MovePosition(rigidbody.position - speed * Time.deltaTime);
        transform.position.z = initPos.z +
            Mathf.PingPong(Time.time * platformSpeed, transform.position.z);
    }
}*/

Having seen what you posted in your "Answer?", there are several things that you should consider that will make your life much easier.

ReturningPlatform.js

var endPoint : Vector3 = Vector3.zero;
private var startPoint : Vector3 = Vector3.zero;
var duration : float = 2.0f;
private var step : float = 0.0f;
private var onPlatform : boolean = false;

function Start() {
    startPoint = transform.position;
}

function FixedUpdate() {
    var delta : float = (Time.deltaTime / duration);
    if(onPlatform) step += delta;
    else step -= delta;
    step = Mathf.Clamp01(step);
    rigidbody.MovePosition(Vector3.Lerp(startPoint, endPoint, step));
    //Or rigidbody.MovePosition(Vector3.Lerp(startPoint, endPoint, 
    //                                       Mathf.SmoothStep(0.0f,1.0f,step)));
}

function OnCollisionEnter(hit : Collision) {
    if(hit.gameObject.tag == "Player") onPlatform = true;
}

function OnCollisionExit(hit : Collision) {
    if(hit.gameObject.tag == "Player") onPlatform = false;
}

If you want it to smoothly start and stop when the player gets on and off mid-flight, then you'd best slow it down before starting up again to avoid sudden changes. This can be easily done by using some adjustments to step's interpolation in those cases, using more state information than just on/off platform, including something like some interpolation state float or some such to see how far you've interpolated from the last motion to the new one.

To create a ping-pong platform is pretty much the same:

PingPongPlatform.js

var endPoint : Vector3 = Vector3.zero;
private var startPoint : Vector3 = Vector3.zero;
var duration : float = 2.0f;
private var step : float = 0.0f;
private var movingBack : boolean = false;

function Start() {
    startPoint = transform.position;
}

function FixedUpdate() {
    var delta : float = (Time.deltaTime / duration);
    if(movingBack) step -= delta;
    else step += delta;
    step = Mathf.Clamp01(step);
    rigidbody.MovePosition(Vector3.Lerp(startPoint, endPoint, step));
    //Or rigidbody.MovePosition(Vector3.Lerp(startPoint, endPoint, 
    //                                       Mathf.SmoothStep(0.0f,1.0f,step)));
    if(step == 1.0f) movingBack = true;
    else if(step == 0.0f) movingBack = false;
}

If you want to trigger/continue motion only on player collision like some sort of a switch, you could do something like:

SwitchPlatform.js

var endPoint : Vector3 = Vector3.zero;
private var startPoint : Vector3 = Vector3.zero;
var duration : float = 2.0f;
var sensitivity : float = 0.1f; //step away to switch direction
private var step : float = 0.0f;
private var onPlatform : boolean = false;
private var movingBack : boolean = false;

function Start() {
    startPoint = transform.position;
}

function FixedUpdate() {
    if(onPlatform) {
        var delta : float = (Time.deltaTime / duration);
        if(movingBack) step -= delta;
        else step += delta;
        step = Mathf.Clamp01(step);
        rigidbody.MovePosition(Vector3.Lerp(startPoint, endPoint, step));
        //Or rigidbody.MovePosition(Vector3.Lerp(startPoint, endPoint, 
    //                                       Mathf.SmoothStep(0.0f,1.0f,step)));
    }
}

function OnCollisionEnter(hit : Collision) {
    if(hit.gameObject.tag == "Player") {
        onPlatform = true;
        if(step >= 1.0f-sensitivity) movingBack = true;
        else if(step <= sensitivity) movingBack = false;
        //Or if you have sensitivity be a distance, you could do:
        //if((transform.position-endPoint).magnitude < sensitivity)
        //    movingBack = true;
        //else if((transform.position-startPoint).magnitude < sensitivity)
        //    movingBack = false;
    }
}

function OnCollisionExit(hit : Collision) {
    if(hit.gameObject.tag == "Player") onPlatform = false;
}

To soften up mid-movement jumps on and off-the platform, you could again add inertia and manipulate the step changes by some function, etc.