Detect player team from a table and add variables to those values

So the title may sound a little weird but basically I’m to both detect a player team name from a table because I don’t want a ton of if statements. I also want to based on which team the player is on will determine the text on a textlable. It might make more sense when you see the script.

Script:

local text = script.Parent.PlayerName
local text2 = script.Parent.PlayerName.PlayerNameShadow
local text3 = script.Parent.TeamText
local text4 = script.Parent.TeamText.TeamTextShadow
local teams = {
	["Clone Trooper"] = "CT",
	["Galactic Marine"] = "GM"
}






local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)
	for index, v in pairs(teams) do
		if player.Team.Name == index then
			text.Text = ''index'-test'
		end
	end

end)

All player instances have a property named “Team” which stores the name of the team the player is in (if they are in a team), you can simply use this to perform what you need.

local text = script.Parent.PlayerName
local text2 = script.Parent.PlayerName.PlayerNameShadow
local text3 = script.Parent.TeamText
local text4 = script.Parent.TeamText.TeamTextShadow

local players = game:GetService("Players")

players.PlayerAdded:Connect(function(player)
	if player.Team == "Clone Trooper" then
		text.Text = player.Team
	elseif player.Team == "Galactic Marine" then
		text.Text = player.Team
	end
end)

https://developer.roblox.com/en-us/api-reference/property/Player/Team
There’s also “TeamColor” if you ever find the need to use it:
https://developer.roblox.com/en-us/api-reference/property/Player/TeamColor

Yes, however, I don’t want to use if statements for all the teams since I will be using many teams. Also for the text part it isn’t the team name, so for example Clone Trooper would be CT.

local text = script.Parent.PlayerName
local text2 = script.Parent.PlayerName.PlayerNameShadow
local text3 = script.Parent.TeamText
local text4 = script.Parent.TeamText.TeamTextShadow

local players = game:GetService("Players")

players.PlayerAdded:Connect(function(player)
	local teamName = tostring(player.Team)
	local strings = string.gsub(teamName, "[%l%s]", "")
	text.Text = strings
end)

This will work for all team names which are formatted similarly to the existing two team names.

This does work for the ones provided, but a few of the others such as ARC Commander would come up as AC according to this method (ARCC would be proper for this). Also, some teams will still have the CT designation even if the team name isn’t Clone Trooper.

local Players	= game:GetService('Players')

local Teams	= {
	['Red'] = 'Team Red',
}

local function FindTeam(TeamName)
	for i, v in pairs(Teams) do
		if i == TeamName then
			return v
		end
	end
end

Players.PlayerAdded:Connect(function(NewPlayer)
	local ShortName = FindTeam(tostring(NewPlayer.Team))
	
	if ShortName then
		-- Do something
	end
end)

How would I set this to the Short part? I tried script.Parent.PlayerName.Text = ShortName but that didn’t work.

-- Services
local Players	= game:GetService('Players')

-- UI Variables
local text		= script.Parent.PlayerName
local text2		= script.Parent.PlayerName.PlayerNameShadow
local text3		= script.Parent.TeamText
local text4		= script.Parent.TeamText.TeamTextShadow

-- Table contains ['PlayerTeamName'] = 'ShortName'
local Teams	= {
	['Clone Trooper']		= 'CT',
	['Galactic Marine']		= 'GM',
}

local function FindTeam(TeamName) -- This function run a loop through the table(Teams) and return the short name. 
	for i, v in pairs(Teams) do
		if i == TeamName then
			return v
		end
	end
end

Players.PlayerAdded:Connect(function(NewPlayer)
	local ShortName = FindTeam(tostring(NewPlayer.Team))
	
	if ShortName then
		text.Text = ShortName
	end
end)

How about this one?

When the player joins and in a team with the name matched inside the table it will return the assigned short name.

1 Like

“ARC Commander” would still be abbreviated as “ARCC”, the string.gsub() is removing all lowercase characters and whitespaces.