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

Help with assigning players to a random team?

I have 3 Teams in my game (Lobby, Blue and Red). When you join the game you are as "Lobby" but when the game starts, I want it to assign random players to a team, either blue are red. Idk how to do this, please help. Thanks!

function AssignTeam()
	for i, v in pairs(game.Players:GetPlayers()) do
		if v:FindFirstChild("TeamColor") == BrickColor.new("Medium stone grey") then
    			--not sure what to put here
		end
	end	
end

2 answers

How to assign players to random teams? Got it. First of all, use GetService for players instead just to be safe. So your line 02 should be for i, v in next, game:GetService('Players'):GetPlayers() do.

Create a variable for one of the team, so the loop knows when to add the player to that team or the other. local redTeam = true;.

For each loop check the new variable and switch it with redTeam = not redTeam.

Also to check for team color use v.TeamColor not v:FindFirstChild("TeamColor")

Whole code:

local redTeam = true;

function AssignTeam()
    for i, v in next, game:GetService('Players'):GetPlayers() do
        if v.TeamColor == BrickColor.new("Medium stone grey") then
                if redTeam then
			v.TeamColor = BrickColor.new('Bright red');
		else
			v.TeamColor = BrickColor.new('Bright blue');
		end
		redTeam = not redTeam;
        end
    end 
end

I assumed you used the default red and blue Brickcolors

4 comments

0

You don't have to get a service through game:GetService() from a serverside script. You should do it from a local script, however, as an exploiter could rename a service and break everything.

0

Why does it put me to the team named "Neutral"?

0

@Link150 Thanks! But I trust in my boi FilterEnabled to prevent exploiters from breaking server stuff.

0

@FiredDusk It should put you in the Bright red and blue teams whatever the team names are. You can change both the teamcolors

This is what i do


local Teams = {'Yellow'.'Girrafe'} -- idk, you can add more teams and this would still work

function ChooseTeam()
local num = math.random(1,#Teams) -- num of items in the table 'Teams'
local ChosenTeam = Teams[num]
print(ChosenTeam) --shud print Yellow or Girrafe

player.TeamColor = game.Teams[ChosenTeam].TeamColor -- assuming you already have the player




end


1 comment

0

This will create unbalanced teams plus you can just fetch the team names from game.Teams instead of having to create the table yourself

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