Hello, im ExoticTwix. Im making a Team Puzzle game. What i want to do is every round that starts it puts players on 4 different teams(all for have 3 players). ive been searching on google and youtube and cant find anything. please help and give my an example script i can use to make every change there teams thanks,
ExoticTwix
You can loop through the players using Players:GetPlayers(). You can use ipairs
to do this which will give you an index and a player. You can use the modulo operator, which returns the remainder for division to determine what team a player should go on.
Example:
-- For each player
local teams = Teams:GetTeams()
local teamNumber = index%(#teams) + 1
local team = teams[teamNumber]
Essentially what this does is it assigns every third player to the first team, the next player to the second team, and the last player to the first team. If you wanted to do this in the reverse order (every third player to third team) you can subtract the number of teams to flip the team number: (#teams - index%(#teams)) + 1
The reason I add 1 is because modulo returns 0 if it’s divisible (no remainder) but arrays in lua start at 1.
just like @Hexcede said above, you could also try and balance the teams out by placing players into teams randomly until the difference between the players in both teams is 1 or 0 because in case there is an odd number of players in the game then it would not be possible and script would probably crash
EDIT: 1 and 0 in my post are not related to the ones in his post, I’m just saying how you can balance the teams or if it is related I’m not aware of that