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

How do you assign 4 even teams?

I have a script that gets 4 teams from Lighting, clones them, puts them in the Teams service, and then (is supposed to) assigns players to it. The problem is, it won't assign players to the teams. This is the script for cloning the teams/map, and moving them to the right place:

			map = game.Lighting.Crossroads:Clone()
			red = game.Lighting.Red:Clone()
			blue = game.Lighting.Blue:Clone()
			green = game.Lighting.Green:Clone()
			yellow = game.Lighting.Yellow:Clone()
			map.Parent = game.Workspace
			red.Parent = game.Teams
			blue.Parent = game.Teams
			green.Parent = game.Teams
			yellow.Parent = game.Teams

That all works fine and dandy, but when I try to assign players to the teams, with:

player = game.Players:GetChildren()
for p = #player, #player do
				if(player[p].TeamColor==BrickColor.new("White"))and (player[p].Character~=nil)then
					player[p].TeamColor = blue.TeamColor
				end
			end
			for p = #player/2, #player do
				if(player[p].TeamColor==blue.TeamColor)and (player[p].Character~=nil)then
					player[p].TeamColor = red.TeamColor
				end
			end
			for p = #player/2, #player do
				if(player[p].TeamColor==red.TeamColor)and (player[p].Character~=nil)then
					player[p].TeamColor = green.TeamColor
				end
			end
			for p = #player/2, #player do
				if(player[p].TeamColor==blue.TeamColor)and (player[p].Character~=nil)then
					player[p].TeamColor = yellow.TeamColor
				end
			end

It doesn't work. I looked in output, and nothing shows up. Please help :)

3 comments

0

this is a fm script, but you want to divide the #players by 4 not 2...

0

I am dividing by 2 because I want to keep people on blue: move all 2 blue, take half of blue to red, take half of red to green, and half of blue to yellow. That would put it into quarters.

0

I need help :(

2 answers

✓ Accepted answer
local players = {}
for a,b in pairs(game.Players:GetChildren()) do
table.insert(players,b)
end

for i = 1,#players do
if i % 1 == 0 then
players[i].TeamColor = BrickColor.new("Bright red") -- change to the color you want team 1
elseif i % 2 == 0 then
players[i].TeamColor = BrickColor.new("Bright blue") -- again change to the color you want
elseif i % 3 == 0 then
players[i].TeamColor = BrickColor.new("Bright yellow") -- again
elseif i % 4 == 0 then
players[i].TeamColor = BrickColor.new("Bright green") -- again
end
end

Should work. I scripted it in here so I never tested it (my studio's down atm and it's working).

5 comments

0

Thanks for the input, though I tested it in Studio and it didn't work. Output doesnt say anything.

0

The trouble is if i%4 is 0, i%2 would also be 0. Plus i%1 is always 0 so it'll always do the first one. The way to fix that is to reverse the order of them.

0

I suppose that would be the problem, but it isn't even assigning me to a team.

0

It doesn't help to reverse the order

0

I don't know then. Sorry

Here is the works: (Extremely inefficient, but HEY, IT WORKS! :D)

local Teams	={green, yellow, red, blue}
function AssignRandomTeam(Player, CurrentTeam)
	for x=1,#Teams do
		if Teams[x] ~= CurrentTeam then
		Player.Parent = Teams[x]
	end
end
function Relocate(Team)
	for i, v in pairs(Team:GetChildren()) do
		AssignRandomTeam(v, Team)
		return 
	end
end
while wait() do
	local NumBlue	= blue:children()
	local NumRed	= red:children()
	local NumYell	=yellow:children()
	local NumGrn	=green:children()
	local TmNums	= {NumGrn, NumYell, NumBlue, NumRed}

	for i = 1, #TmNums do
		if #TmNums[i] > #TmNums/game.Players.NumOfPlayers then
			Relocate(Teams[i])
		end
	end
end

1 comment

0

PS I'm sorry if I screwed up, I am rusty on Teams and such

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