How to make someone change team when they join group

How do I make someone change team when they join group


When someone joined the group and is first(or more) rank in group, I want them to have the same rank in the game as they have in group. For example, If someone is "EXAMPLE" rank in group then in game in the teams section they are the same rank. I've been trying to do this for a long time.
Please answer below!
1 Like

Hey there.
I’ve seen this question before.
Check it out.

3 Likes

It’s a rather old topic, but should still work.

1 Like

From March 7th? Of this year? That’s not that old is it?

1 Like

Ah. A simpler time. Feels old for me, I’ve learned quite a bit since then. Good luck to both of you!

1 Like

And you :slight_smile: Also @Gucci_Dabs222 if you feel that is the solution, then feel free to mark the question as solved. Any further questions regarding the script, just ping me or reply to the topic. And good luck of course.

2 Likes

Okay, I will try out the script and then I will go make your reply the solution.

Whichever reply you feel is best :-). Although I do recommend also reading up on the specific functions that will help you learn while scripting.

1 Like

I do know a tiny bit about scripting so I think I can work it out.

@MFCmaster1234 It didn’t work, Here is the script and how I edited it to match up the group and teams.

local Team1 = game:GetService(“Teams”):WaitForChild(“Soldier {PVT}”)
local Team2 = game:GetService(“Teams”):WaitForChild(“Air Force”)
local Team3 = game:GetService(“Teams”):WaitForChild(“Gate Guards”)
local Team4 = game:GetService(“Teams”):WaitForChild(“Major”)
local Team5 = game:GetService(“Teams”):WaitForChild(“NNA Director”)
local Team6 = game:GetService(“Teams”):WaitForChild(“Owner”)
local Team7 = game:GetService(“Teams”):WaitForChild(“Civilian”)

local Group = 6887105
local Rank1 = 250
local Rank2 = 251
local Rank3 = 252
local Rank4 = 253
local Rank5 = 254
local Rank6 = 255
local Rank7 = 0

function onPlayerAdded(Player)
local PlayerRank = Player:GetRankInGroup(Group)
if PlayerRank >= Rank1 then
Player.Team = Team1
if PlayerRank >= Rank2 then
Player.Team = Team2
if PlayerRank >= Rank3 then
Player.Team = Team3
if PlayerRank >= Rank4 then
Player.Team = Team4
if PlayerRank >= Rank5 then
Player.Team = Team5
if PlayerRank >= Rank6 then
Player.Team = Team6
if PlayerRank >= Rank7 then
Player.Team = Team7
end
end
end
end
end
end
end
end

game:GetService(“Players”).PlayerAdded:Connect(onPlayerAdded)

I’d recommend a few things. A. Let’s make use of some arrays, so as to avoid using 3 separate variables, and comparing them all to each other. Also, printing is your best friend when locating an issue in your code.
Let’s add a print or two to figure out if the script even gets the proper rank.

So essentially,

--[[
local Team1 = game:GetService(“Teams”):WaitForChild(“Soldier {PVT}”)
local Team2 = game:GetService(“Teams”):WaitForChild(“Air Force”)
local Team3 = game:GetService(“Teams”):WaitForChild(“Gate Guards”)
local Team4 = game:GetService(“Teams”):WaitForChild(“Major”)
local Team5 = game:GetService(“Teams”):WaitForChild(“NNA Director”)
local Team6 = game:GetService(“Teams”):WaitForChild(“Owner”)
local Team7 = game:GetService(“Teams”):WaitForChild(“Civilian”)
--]]
--All of that above can be reduced to a single array containing references to the teams. 
local Teams= game:GetService("Teams")
--local Group = 6887105
--There really isn't a reason to make the GroupID a variable when you only need to use it a single time.
--[[
local 
local Rank2 = 251
local Rank3 = 252
local Rank4 = 253
local Rank5 = 254
local Rank6 = 255
local Rank7 = 0
--]]
-- Again, these can all simply be added to a single array, and since we know which Rank Number goes with which Team in the game, we can combine the two together here, thus reducing the amount of code necessary.
local RankTable = {["Soldier {PVT}"] = 250, [“Air Force”] = 251, [“Gate Guards”] = 252, ["Major"] = 253,["NNA Director"] = 254,["Owner"] = 255,["Civilian"] = 0}
--now we get the rank in group and see if the player's rank can be found in the group. If we find one that is equal, we set the player's team to the Team Name for that rank.
function onPlayerAdded(Player)
local PlayerRank = Player:GetRankInGroup(6887105)
print(“The Player’s rank one the group is “,PlayerRank)
for i,v in pairs(RankTable) do
if v == PlayerRank then
Player.Team =  Teams:FindFirstChild(i)
end
end
end
-- I can reduce this even more But I am in a bit of a time crunch right now.
game:GetService(“Players”).PlayerAdded:Connect(onPlayerAdded)
1 Like

Keep in mind @Gucci_Dabs222 All I have done is optimize your own code, and eliminating possible errors in the If then nightmare chain. Too many ifs can ruin a script, because you start losing track of what code will run and you have to print too many times.

1 Like

Okay ill try it. I will tell you if it works.

1 Like

Try using Output when you run the game so you can see the print results. I forgot to edit the script so your group ID is in the GetPlayerRank b it. Just copy paste that into the place where it says Group

1 Like

YES IT WORKS! Thank You @MFCmaster1234!

Of course. Now I will recommend adding some sanity checks in there. For example a player may be a rank that you haven’t added to the table yet and this would break the script. As a quick fix, if you reverse the order of the table I made above, you can reduce the amount of code even more while adding a sanity check inherent by virtue of the rank check.
if you switch the RankTable’s keys and values above, you can then simply check to see if the rank number is in the table as an index instead of needing to iterate through the table looking at each pair.

local RankTable = {[250] = "Soldier {PVT}",[251] = "Air Force",[252] = "Gate Guards",[253] = "Major",[254] = "NNA Director",[255] = "Owner",[0] = "Civilian"}
function onPlayerAdded(Player)
local PlayerRank = Player:GetRankInGroup(6887105)
print(“The Player’s rank one the group is “,PlayerRank)
if RankTable[PlayerRank] then
Player.Team =  Teams:FindFirstChild(RankTable[PlayerRank])
end
end
game:GetService(“Players”).PlayerAdded:Connect(onPlayerAdded)

Of course, this will require you to manually add each of the ranks to your table. But there is a solution to that as well.

1 Like

Here it is, the final, optimized code(s).
The issues with the last code lay in the requirement to create a table in order to check the role in the group. Now this is nice, but considering we are already making a request to the group, to get an int64 value (the rank number), then comparing it to the string, which is the role name, we can just use
GetRoleInGroup() to essentially do the job for us with one call. Returning a string which is the name of the Role itself, thus eliminating the need for a table at all, and only requiring us to name our Teams in game the exact names of the roles in the group.
Of course we can always use the table method, should we decide we want to name the team something different in the game than we do in the Group. However, if our team names are going to be based directly on rank, then we can cut out our variables entirely, as the Output will tell us if the player’s role is not actually a team in game.
To review, the difference between the previous code I posted and this one, is that it relies entirely on the presumption that all your team names are going to be the same as all your group names. You can of course change this by simply adding a dictionary array that has the Rank Names as keys and equates them to the Team Names in your game.

This is the method you use if you want the Team Names to be exactly the same as the Group Role Names

game:GetService("Players").PlayerAdded:Connect(function(player)
player.Team = game:GetService("Teams")[player:GetRoleInGroup(6887105)]
end)

Otherwise use this method if you want to name them something different, the keys will be the Group Role Names, and the Values will be the team names.

local TeamNames = {["Owner"] = "Supreme Overlord", ["Civilian"] = "Peasant"}
game:GetService("Players").PlayerAdded:Connect(function(player)
player.Team = game:GetService("Teams")[TeamNames[player:GetRoleInGroup(6887105)]]
end)
2 Likes