The paper deals with the consensus problem in a leaderless network of agents that have to reach a common velocity while forming a uniformly spaced string. Moreover, the final common velocity (reference velocity) is determined by the agents in a distributed and leaderless way. Then, the consensus protocol parameters are optimized for networks characterized by a communication topology described by a class of directed graphs having a directed spanning tree, in order to maximize the convergence rate and avoid oscillations. The advantages of the optimized consensus protocol are enlightened by some simulation results and comparison with a protocol proposed in the related literature. The presented protocol can be applied to coordinate agents such as mobile robots, automated guided vehicles (AGVs) and autonomous vehicles that have to move with the same velocity and a common inter-space gap.