How do I get a team name onto a text label for my GUI?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    Make it display a player’s team on a GUI on my custom playerlist.

  2. What is the issue? Include screenshots / videos if possible!
    I have no idea what types of text I need to type into a script to display a player’s team.

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I’ve tried looking for different YouTube videos and different DevForums but nothing helped.

I’m not asking for someone to type a full script out. I would just like for someone to point me in the right direction to get a working script!
Thank you. :slight_smile:

3 Likes
local textLabel = script.Parent -- change to your right path
local teams = game:GetService("Teams")

local players = game:GetService("Players")
local player = players.LocalPlayer

--initial apply
textLabel.Text = player.Team.Name
--when the player changes team , apply the change
player:GetPropertyChangedSignal("Team"):Connect(function()
	textLabel.Text = player.Team.Name
end)

This is just an example [on local side]

2 Likes

Thank you for your reply!
I’ve only got it to partially work. The text label’s text is replaced with the first team that the players spawns in as, but when the team is changed, the text label doesn’t change (I feel like this could be fixed by looping the script though). Something that I’ve also noticed is that the script is crashing the game in studio and when the game is published, it won’t start the game at all.

1 Like

This has already been applied in the last part of @Valkyrop 's script.

player:GetPropertyChangedSignal("Team"):Connect(function()
	textLabel.Text = player.Team.Name
end)
-- unless teams are being changed client-sided and this code is in a server script, there shouldn't be any problem

I don’t think this is related to the script that was given. You should check through other scripts that you believe could be causing such behaviour.

2 Likes
local __PLAYER = game:GetService("Players"):GetPlayerFromCharacter(script.Parent.Parent)
local __TEAMS = game:GetService("Teams")


script.Parent.Frame:WaitForChild("Team").Text = __PLAYER.Team.Name
__PLAYER:GetPropertyChangedSignal("TeamColor"):Connect(function()
	
	if __PLAYER.Team == __TEAMS["Visitors"] then
		script.Parent.Frame:WaitForChild("Team").Text = "Visitor"
	elseif __PLAYER.Team == __TEAMS["Students"] then
		script.Parent.Frame:WaitForChild("Team").Text = "Student"
	elseif __PLAYER.Team == __TEAMS["Staff"] then
		script.Parent.Frame:WaitForChild("Team").Text = "Staff"
	elseif __PLAYER.Team == __TEAMS["Council"] then
		script.Parent.Frame:WaitForChild("Team").Text = "Council"
	elseif __PLAYER.Team == __TEAMS["Owner"] then
		script.Parent.Frame:WaitForChild("Team").Text = "Owner"
	else
		script.Parent.Frame:WaitForChild("Team").Text = "No Team"
	end
end)

You can just edit the script to work with your teams.

Put this in a regular script inside the team text label, edit the path to script.Parent
also remember to edit the path to the player

1 Like

This seems too much for a simple script. A better way to do this is to instead use tables to keep the script neat and organized.

-- code has been edited

local Players = game:GetService("Players")
local Teams = game:GetService("Teams")

local player = Players.LocalPlayer
local TextLabel = script.Parent 

-- this is all just an example

local listOfTeamNames = { -- you can list the Team Instances, Team Names, or Team Colors here
	"Team1", -- change these strings to the actual name of your teams
	"Team2"
	-- etc.
}

local listOfStrings = {}
listOfStrings["Team1"] = "TEXT TO DISPLAY FOR TEAM1" -- display text for this team
listOfStrings["Team2"] = "TEXT TO DISPLAY FOR TEAM2"
-- if you have more teams you'd like to add, just rewrite the line of code above and make sure to set the first string as the team name


for index, teamNames in pairs(listOfTeamNames) do
	if player.TeamColor == teamNames then
		TextLabel.Text = listOfStrings[player.Team.Name]
	end
end

player:GetPropertyChangedSignal("TeamColor"):Connect(function()
	for index, teamNames in pairs(listOfTeamNames) do
		if player.TeamColor == teamNames then
			TextLabel.Text = listOfStrings[player.Team.Name]
		end
	end
end)

Also, if the TextLabel is under a GuiObject, it’s better to have the text changed via LocalScript to avoid any confusion between server and client accessibility.

Thank you. I’m assuming this script will be inside of the TextLabel that I want to change?

1 Like

Yes. Make sure it’s a local script. I’m going to rewrite the script above so it is a bit more easier for you to understand without any confusion. Should take about a minute (I’m going to edit the post above).

Unfortunately, it still doesn’t work.

I don’t want to be a pain, but would it be possible for me to upload the file for you to take a look at it?

Sure, you can upload the file. I will just need to see the TextLabel and all your existing teams.

Thank you! I appreciate it.
All of the teams are listed at the bottom of the script in the “Job” TextLabel.
PlayerListGui.rbxm (13.1 KB)

1 Like

Here is your updated code:

local Players = game:GetService("Players")
local Teams = game:GetService("Teams")

local player = Players.LocalPlayer
local TextLabel = script.Parent

local listOfTeamNames = { -- you can list the Team Instances, Team Names, or Team Colors here
	"Farmer",
	"Mechanic",
	"Unemployed"
}

local listOfStrings = {}
listOfStrings["Farmer"] = "Occupation: Farmer" -- If you intend to change the display text, change it right here
listOfStrings["Mechanic"] = "Occupation: Mechanic" -- same goes for the rest of the teams
listOfStrings["Unemployed"] = "Occupation: Unemployed"

--[[
if you have more teams you'd like to add, first make sure to add an extra string
in the 'listOfTeamNames' table first, then rewrite the line of code above and
change the first string to the name of your new team

Example of adding new teams:

local listOfTeamNames = {
	"Farmer",
	"Mechanic",
	"Unemployed", -- add a comma or semicolon after the previous string to add a new string in the table
	"Engineer" -- new team name
}

local listOfStrings = {}
listOfStrings["Farmer"] = "Occupation: Farmer"
listOfStrings["Mechanic"] = "Occupation: Mechanic"
listOfStrings["Unemployed"] = "Occupation: Unemployed"
listOfStrings["Engineer"] = "Occupation: Engineer" -- new team

]]


for index, teamNames in pairs(listOfTeamNames) do
	if player.Team.Name == teamNames then
		TextLabel.Text = listOfStrings[player.Team.Name]
	end
end

player:GetPropertyChangedSignal("TeamColor"):Connect(function()
	for index, teamNames in pairs(listOfTeamNames) do
		if player.Team.Name == teamNames then
			TextLabel.Text = listOfStrings[player.Team.Name]
		end
	end
end)



-- All teams (for now): Farmer, Mechanic, Unemployed

The reason why it didn’t work was because you did not change the strings to the name of your teams.

Edit:

I’ve found a little mistake I made in the code while testing, I have just fixed the code and tested it in studio. I’ve edited the code in this post. It should be working fine now.

It still isn’t changing the text. I’m not really sure what’s going on. (The team is definitely changing but it isn’t being displayed)

I’ve just applied another change to the code in my previous post. Copy and paste the code again.

It’s working now!
Thank you so much! :slight_smile:

1 Like

You’re welcome, happy to help!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.