If it works off the engine, but not on it, you should consider the possibility of minor damage to the entry side of the teeth of either the pinion or the ring gear. In the case of the ring gear, remember that the engine's rest position is not entirely random: it won't stop during the latter part of the compression stroke, for example, but may be particularly likely to stop near the beginning of it.