Auto Team with Mutiple Groups

Hey there! I apologise if this has been solved before but I have looked everywhere and not been able to achieve what I wanted.

I am trying to make a script that teams a player based on what groups they are in, the problem is, is that there are many groups going to one team and players might be in two of those same groups. I own a little military group which has a main group, division groups, then unit groups and I want to team a player based on their division. I had some code written and placed it into a local script on startcharacterscripts but it still didnt work. I checked the output but nothing was written there.

I know it is very messy and any help would be much appreciated! Sorry if this has been asked before.

function onPlayerEntered(plr)
	local Fighter = 9503940
	local BCT = 9503920
	local SOW = 9507285
	local OTS = 9503903
	local STG = 9503909
	local AFSOC = 9503738
	local ACC = 9503755
	local AETC = 9503758
	local Main = 8788176



	if plr:IsInGroup(Fighter) then
		plr.TeamColor = BrickColor.new("Really black")
	else if plr:IsInGroup(ACC) then 
			plr.TeamColor = BrickColor.new("Really black")
		else if plr:IsInGroup(BCT) then 
				plr.TeamColor = BrickColor.new("Deep blue")
			else if plr:IsInGroup(OTS) then 
					plr.TeamColor = BrickColor.new("Deep blue")
				else if plr:IsInGroup(AETC) then 
						plr.TeamColor = BrickColor.new("Deep blue")
					else if plr:IsInGroup(SOW) then 
							plr.TeamColor = BrickColor.new("Neon orange")
						else if plr:IsInGroup(STG) then 
								plr.TeamColor = BrickColor.new("Neon orange")
							else if plr:IsInGroup(AFSOC) then 
									plr.TeamColor = BrickColor.new("Neon orange")
								else if plr:IsInGroup(Main) then if plr:GetRankInGroup(Main) == 1 then
											plr.TeamColor = BrickColor.new("Really red")
										else if plr:IsInGroup(Main) then if plr:GetRankInGroup(Main) >= 20 then
													plr.TeamColor = BrickColor.new("CGA Brown")
												else if plr:IsInGroup(Main) then
														plr.TeamColor = BrickColor.new("Earth green")


														plr:LoadCharacter()
													end
												end
											end
										end
									end
								end
							end
						end
					end
				end
			end
		end
	end
end

game.Players.PlayerAdded:connect(onPlayerEntered)
2 Likes

you should use “Script” instead of localscript for it to work better.

local colors = {
	[9503940] = BrickColor.new("Really black"),
    [9503920] = BrickColor.new("Really black"),
	-- ["GROUP ID"] = Color
}
game.Players.PlayerAdded:connect(function(plr)
      for i,v in pairs(colors) do
           if plr:GetRankInGroup(i) then
	            plr.TeamColor = v
           end
       end
end)

you have to make teams and similar for it to work btw

1 Like

Alrighty. Ill check it out when I get some time!

So this does work, however in the main group I need it to know to team a certain rank (20 and up) to a team headquarters and in the main group I wanna team rank 1 to BCT team. I am trying to make it so if you are in a division you get teamed to that division, if you are in rank 1 you get teamed to BCT, if you are not in a division you get teamed to “USAF Personnel” and if you are rank 20 or higher you get teamed to HQ. Sorry if its confusing and I should have specified this earlier.

you can just add checks for ranks in group. like

if plr:GetRankInGroup(group) == 1 then
      plr.TeamColor = this
end

Ok, and would I add that at the end or?