AddPlayer is not a valid member of Team

I have run into a problem, trying to script a “AutoTeam” script.
What I am trying to do is automaticly teaming a player based on their Group and Rank
This is my script:

local GROUP_DATA = {
	{GroupID = 17178529, MinRank = 10, MaxRank = 255, TeamName = "Headquarters"},
	{GroupID = 789012, MinRank = 20, MaxRank = 80, TeamName = "Civilian"},
	{GroupID = 345678, MinRank = 30, MaxRank = 100, TeamName = "Jailed"}
}

local function playerInRankRange(player, groupData)
	local playerRank = player:GetRankInGroup(groupData.GroupID)
	return (playerRank >= groupData.MinRank) and (playerRank <= groupData.MaxRank)
end

game.Players.PlayerAdded:Connect(function(player)
	print("Player " .. player.Name .. " joined the game.")

	for _, groupData in ipairs(GROUP_DATA) do
		if playerInRankRange(player, groupData) then
			local team = game:GetService("Teams"):FindFirstChild(groupData.TeamName)
			if team then
				team:AddPlayer(player)
				print("Player " .. player.Name .. " assigned to team " .. groupData.TeamName .. " (Group ID " .. groupData.GroupID .. ", Rank range " .. groupData.MinRank .. " - " .. groupData.MaxRank .. ").")
				break
			else
				warn("Error: Team not found for group " .. groupData.GroupID .. ", rank range " .. groupData.MinRank .. " - " .. groupData.MaxRank .. ", team name " .. groupData.TeamName)
			end
		end
	end
end)

I get the output “AddPlayer is not a valid member of Team”, the script is a normal script located inside Serverscriptservice

2 Likes

Note that I already have tried looking at the Team names, and they are correct

Team:AddPlayer is not a method that exists; if you want to add a player to a team, you have to assign the Team property in the player Instance to the Team Instance.

Well, it seems to have stopped the error in the output. But the script still doesn’t work:((

-- Replace these values with your own group IDs, rank ranges, and team names
local GROUP_DATA = {
	{GroupID = 17178529, MinRank = 10, MaxRank = 50, TeamName = "Headquarters"},
	{GroupID = 789012, MinRank = 20, MaxRank = 80, TeamName = "Civilian"},
	{GroupID = 345678, MinRank = 30, MaxRank = 100, TeamName = "Jailed"}
}

-- Function to check if a player is within a specific group's rank range
local function playerInRankRange(player, groupData)
	local playerRank = player:GetRankInGroup(groupData.GroupID)
	return (playerRank >= groupData.MinRank) and (playerRank <= groupData.MaxRank)
end

-- Event handler for when a player joins the game
game.Players.PlayerAdded:Connect(function(player)
	-- Iterate through all group data and assign player to team if within rank range
	for _, groupData in ipairs(GROUP_DATA) do
		if playerInRankRange(player, groupData) then
			local team = game:GetService("Teams"):FindFirstChild(groupData.TeamName)
			if team then
				player.Character.Humanoid.Team = team
				break
			else
				print("Error: Team not found for group " .. groupData.GroupID .. ", rank range " .. groupData.MinRank .. " - " .. groupData.MaxRank .. ", team name " .. groupData.TeamName)
			end
		end
	end
end)

This should be:

if team then
	player.Team = team
    break
else
    print("Error: Team not found for group " .. groupData.GroupID .. ", rank range " .. groupData.MinRank .. " - " .. groupData.MaxRank .. ", team name " .. groupData.TeamName)
end