It's probably the vehicle speed sensor. It's located on top of the rear differential.
When the sensor isn't working right the speedometer doesn't work and the computer doesn't know how fast the truck is going, either. It needs to know this to know when and how to shift the transmission.
A new sensor is well under $20. I recommend changing it. It's an easy 5 minute job. There is one electrical connector and one bolt.