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

Assigning players to a specified team?

Hello!

I just made a very basic clicker game, and added a localscript which can detect a players country when joining the game (exactly this : https://developer.roblox.com/en-us/api-reference/function/LocalizationService/GetCountryRegionForPlayerAsync ), i put it in the Starterplayerscripts because its the only way it works. So the main thing is : i want to assign players automatically to a specified team by their country, like if a player joins from Canada then he will be assigned to the "Canada" team, i want to do this but with like 100+ teams(countries). I tried everything today, i just cant figure it out to work sadly. Can somebody please help me to do it? Much thanks!

1 answer

What you could do is check if the Team/Country already exists in the Team service, in the scenario where it doesn't, it'll create an entire new team and place the player inside of that team, but if the team does already exist, it'll place them in the team.

local players = game:GetService("Players")
local LS = game:GetService("LocalizationService")
local teams = game.Teams

local function createTeam(name)
	local newTeam = Instance.new("Team")
	newTeam.Name = name
	newTeam.Parent = teams
	return newTeam
end

players.PlayerAdded:Connect(function(player)
	local foundRegion = LS:GetCountryRegionForPlayerAsync(player)
	if teams:FindFirstChild(foundRegion) == nil then
		local createdTeam = createTeam(foundRegion)
		player.Team = createdTeam
	else 
		player.Team = teams:FindFirstChild(foundRegion)
	end
end)

I tested this and it worked. I'm not the best scripter, but hopefully this helps!

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