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

Why is this script not giving players a team and no errors?

How do i make players get a random team?

local teamColors = {BrickColor.new("Bright red"), BrickColor.new("Bright blue")}	
for i, user in pairs(game:GetService("Players"):GetPlayers()) do
if math.random(1, 2) == 1 then
user.TeamColor = BrickColor.new('Bright blue')
else
user.TeamColor = BrickColor.new('Bright red')
end
end

1 answer

Problem

You want to make a player get a random team

Solution

Use the PlayerAdded function to work this out with each joining player. Also remove the teamColors array because it seems to not be in use.

Final Script

--Function
function PlayerAdded(user)
	if math.random(1, 2) == 1 then
		user.TeamColor = BrickColor.new('Bright blue')
	else
		user.TeamColor = BrickColor.new('Bright red')
	end
end

game:GetService('Players').PlayerAdded:connect(PlayerAdded)

--Since studio changed the way PlayerAdded works, put these lines to make it work in Solo mode
for i, user in ipairs(game:GetService('Players'):GetPlayers()) do
	PlayerAdded(user)
end

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