Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

If i re-enable AutoAssignable at a team, will the players team change randomly?

Hello, I was searching for a way to make players change team randomly after a match ends. I have an idea of disabling AutoAssignable, setting every players team to nill and enabling AutoAssignable after wait(). Will this method work?

local players = plrs:GetChildren()
		
for i = 1, #players do
	local plr = players[i]
	plr.Neutral = true
	plr.Team = nil
end

wait()
teams.Red.AutoAssignable = true
teams.Blue.AutoAssignable = true
		
local players = plrs:GetChildren()
		
for i = 1, #players do
	local plr = players[i]
	plr.Neutral = false
end

1 answer

✓ Accepted answer

No, the AutoAssignable Property of Teams only refers to when the Players join the game, so if you changed this only new players to the server would be randomly assigned.

Instead you can use the :GetPlayers() function of the Players Service to get all players in a table, then iterate over this in a for-loop.

The Teams Service has a function called :GetTeams() which gives you a table of all teams.

When you want to randomly assign a player, you can make a random number between 1 and the number of teams and place a player on the corresponding team like so:

for i, v in pairs(game:GetService("Players"):GetPlayers()) do
	local allteams = game:GetService("Teams"):GetTeams()
	local random = math.random(1, #allteams)
	v.Team = allteams[random]
end

It is also generally preferred not to use :GetChildren() to retrieve player instances since if an object that was named the same as the player was added to the service you may grab it by mistake with :FindFirstChild() or in this case, would receive an error because parts don't have Team Properties

This discussion is preserved as it was. New answers, comments, and votes are closed.