Just a quick thought:

Check out these functions

GetPlayerCount

IterateParty