How to make a match making system like bedwar

I made a cross server match making system already which will teleport 2 players into a reserve server even if they were not in the same server when teleporting . However, now that i have the two players in the same server, I have a problem where I have no idea how to make a team management system .

There will be three teams , blue , red and spectator . I am planning to make a spectate match function which will allow random players to teleport into match and spectate the fight . However, as of now, I have no idea how to make a team management script . Anybody can help? Ty