You aren't doing anything wrong, but that is normal C3 behavior. If there are no instances, there is nothing to get the width from.
The first time you try to get the width right after calling the function that creates an instance, the new instance is not fully created yet (which happens after the next top level event). Since there are no pickable instances at that point, it returns a width of 0.
if you add the width to xposition inside the function, while the new instance is still active, it will work fine. You could also add a "Wait 0" after the function call, before you add the width, and that will work as well (because that gives C3 a chance to finish creating the instance).