Attaching dynamic code to a button

looking for help

i need to add code to attached buttons

for(i=1; i<=6; i++) {
_root.attachMovie(“niceButton”, “niceButton”+i, this.getNextHighestDepth(), {_x: 50 * i, _y: 50});
_root[“niceButton”+i].onPress = function() {
trace(i);
}
}

this traces “7” for every button’s onPress!
what i want is for each buttons to trace from 1 to 6, according to each attach.
i’ve tried multiple version of the code but i either get undefined or “7”
the result i’m after is the following code to be attached to each button:

_root.niceButton1.onPress = function() {
trace(“1”);
}

i hope someone knows and can enlighten me on this please
thanks