Creating a custom nametag for player that are on certain teams

So basically I wanted to create a name tag based on the team you are in. Ex: player: JohnDoe team: Blue that mean he will have a nametag below his player name, that says ‘blue’. I have already attempted at trying this, and no errors, but the doesn’t work

-- Serverscript in serverscriptservice
--Variables
local rep = game:GetService("ReplicatedStorage") 
local nametag = rep.NameTag 

--Functions
for _, player in pairs(game.Players:GetPlayers()) do
local char = player.Character
		--Varibles
		local head = char.Head
		local newtext = nametag:Clone() --Cloning the text.
		local uppertext = newtext.UpperText
		local lowertext = newtext.LowerText
		local humanoid = char.Humanoid
		
		humanoid.DisplayDistanceType = "None"
		
		--Main Text
		newtext.Parent = head
		newtext.Adornee = head
		uppertext.Text = player.Name --Changes the text to the player's name.
		
		--"If" Statements
		if player.Team.Name == "Joe Team" then
			lowertext.Text = "Joe Team" --This is that the text will say.
			lowertext.TextColor3 = Color3.fromRGB(255, 51, 0) --This is what the color of the text will be.
		end
	end
2 Likes

Is nametag on line 6 defined somewhere?

Also, are you running this right when the game starts? It probably won’t work, as the players won’t be loaded so nothing will happen. Try using CharacterAdded.

Yep I have a thing called in rep, and I want this to loop every sec, I can figure that out
EDIT:

--Variables
local rep = game:GetService("ReplicatedStorage") --You can change this to ServerStorage for more security.
local nametag = rep.NameTag 

while true do
--Functions
for _, player in pairs(game.Players:GetPlayers()) do
local char = player.Character
		--Varibles
		local head = char.Head
		local newtext = nametag:Clone() --Cloning the text.
		local uppertext = newtext.UpperText
		local lowertext = newtext.LowerText
		local humanoid = char.Humanoid
		
		humanoid.DisplayDistanceType = "None"
		
		--Main Text
		newtext.Parent = head
		newtext.Adornee = head
		uppertext.Text = player.Name --Changes the text to the player's name.
		
		--"If" Statements
		--You can add as many of these as you wish, just change it to the player's name.
		if player.Team.Name == "Joe Team" then
			lowertext.Text = "Joe Team" --This is that the text will say.
			lowertext.TextColor3 = Color3.fromRGB(255, 51, 0) --This is what the color of the text will be.
		end 
       wait(2)
	end
end

What is nametag, a BillboardGui?

Also, I don’t recommend looping. CharacterAdded would be better.

Also, be sure to check that the character exists if you are going to be looping.

yes nametag is a billboard gui, and also, charater added is for local script, I’m using a serverscript

This worked for me.

Make sure to wait for the character by using local char = game.Workspace:WaitForChild(player.Name).

Again, I don’t recommend the looping method, but it will work.

Also, you should move your wait() statement so it’s right before the last end, otherwise every single player will take 2 seconds.

And you will probably need to change the offset of the GUI so it’s above the character’s head.

--Variables
local rep = game:GetService("ReplicatedStorage") --You can change this to ServerStorage for more security.
local nametag = rep.NameTag 

while true do
--Functions
for _, player in pairs(game.Players:GetPlayers()) do
local char = player:WaitForChild("Character")
		--Varibles
		local head = char.Head
		local newtext = nametag:Clone() --Cloning the text.
		local uppertext = newtext.UpperText
		local lowertext = newtext.LowerText
		local humanoid = char.Humanoid
		
		humanoid.DisplayDistanceType = "None"
		
		--Main Text
		newtext.Parent = head
		newtext.Adornee = head
		uppertext.Text = player.Name --Changes the text to the player's name.
		
		--"If" Statements
		--You can add as many of these as you wish, just change it to the player's name.
		if player.Team.Name == "Joe Team" then
			lowertext.Text = "Joe Team" --This is that the text will say.
			lowertext.TextColor3 = Color3.fromRGB(255, 51, 0) --This is what the color of the text will be.
		end 
       wait(2)
	end
end

It causes a script timeout

This is incorrect.

CharacterAdded works across all scripts.

Ok, thanks for the info, but either way I can’t use it because the player chooses the team via Gui

You can use a RemoteEvent to tell the server that the player has chosen a team. Then assign the team and create the text label above their name.

In a CharacterAdded event you can check if they have a team already. If they do, then create the label.

Like this?

-- serverscript in serverscriptservice
--Variables
local rep = game:GetService("ReplicatedStorage") --You can change this to ServerStorage for more security.
local nametag = rep.NameTag 

-- functions
game.ReplicatedStorage.GoToJoeTeam.OnServerEvent:Connect(function(player)
--Varibles
local char = player.Character
local head = char.Head
local newtext = nametag:Clone() --Cloning the text.
local uppertext = newtext.UpperText
local lowertext = newtext.LowerText
local humanoid = char.Humanoid
		
humanoid.DisplayDistanceType = "None"
		
--Main Text
newtext.Parent = head
newtext.Adornee = head
uppertext.Text = player.Name --Changes the text to the player's name.
		
--"If" Statements
--You can add as many of these as you wish, just change it to the player's name.
if player.Team.Name == "Joe" then
lowertext.Text = "Joe" --This is that the text will say.
lowertext.TextColor3 = Color3.fromRGB(255, 51, 0) --This is what the color of the text will be.
end 
end)
-- local script in gui button
script.Parent.MouseButton1Click:Connect(function()
      game.ReplcatedStorage.GoToJoeTeam:FireServer()
end)

I feel like this a much simpler way to do it. It assigns your team not just a specific team and correct me if I’m wrong but you want it to display the players team so I came up with this.

--// Services
local players = game:GetService("Players")
local replicatedStorage = game:GetService("ReplicatedStorage")

--// Assets
local nametag = replicatedStorage.NameTag 

--// Apply Name Tag
local function SetNameTag(player)
	local char = player.Character
	local head = char.Head
	local newtext = nametag:Clone() -- Get new name tag
	local uppertext = newtext.UpperText
	local lowertext = newtext.LowerText
	local humanoid = char.Humanoid
	humanoid.DisplayDistanceType = "None"
	
	--// Name Tag
	newtext.Parent = head
	newtext.Adornee = head
	uppertext.Text = player.Name -- Set text to players name
	
	--// Set Text
	lowertext.Text = player.Team.Name -- Set text to team name.
	lowertext.TextColor3 = player.TeamColor.Color -- Set text to team color.
end

--// Player Added
players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(char)
		SetNameTag(player)
	end)
end)

--// For players already in game.
for _, player in pairs(players:GetPlayers()) do
	if (player.Character) then
		SetNameTag(player)
	end
end
1 Like

The problem with my script is that I can’t do multiple teams
@IAmKingMatt @imalex4 @d_vHoodie

Is that what you wanted? The one I made is for multiple teams.

14:04:20.529 - Color is not a valid member of Team

My Mistake it’s

lowertext.TextColor3 = player.TeamColor.Color -- Set text to team color.

Just set text to the color of the player’s team.

How can players be in game before script from ServerScriptService start running.

It’s possible, it’s called redundancy and I’ve experienced problems with not making the for loop on the PlayerAdded.

You can’t use brickcolor for TextColor3

1 Like