Name tag script not working quite right

I made a custom name-tag script for my game where higher-ranked users could edit the bottom text of a players name-tag, for example:

This is a user’s normal rank, but using the command “!role jaxsnjake test”, their name-tag should look like this:

My problem is, for some reason, this script doesn’t work for all people, for example, my main account never has a nametag above their head, and when using the “!role” command, nothing happens.

image

My question is, why is this happening, and how can I fix this so that all players get a name-tag above their head?

Name tag script:


local GroupId = 6708836

game.Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Character)
		local Head = Character:WaitForChild("Head")
		local NameTag = NameTagTemplate:Clone()
		NameTag.Parent = Head
		
		NameTag.NameText.Text = Player.Name
		NameTag.RankText.Text = Player:GetRoleInGroup(GroupId)
		
		Character.Humanoid.DisplayDistanceType = Enum.HumanoidDisplayDistanceType.None
	end)
end)

!role command script:

local MinimumRankToUseCommand = 8

game.Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Character)
	end)
	
	Player.Chatted:Connect(function(Message)
		local SplitMessage = Message:split(" ")
		if SplitMessage[1] == "!role" and Player:GetRankInGroup(GroupId) >= MinimumRankToUseCommand then
			local NameOfPlayerToWarn = SplitMessage[2]
			local PlayerToChange = game.Players:FindFirstChild(NameOfPlayerToWarn)
			local Reason = Message:split(NameOfPlayerToWarn)[2]
			local Head = PlayerToChange.Character:WaitForChild("Head")
			
			local WarningGUI = PlayerToChange.Character.Head.NameTag.RankText
			WarningGUI.Text = Reason
		end
	end)
end)```
1 Like

I did some more investigation and found out that the !role command script works fine, and it’s the name tag script that isn’t working. Could anyone help me figure out what’s wrong with it?

1 Like

Yes, It’s prob the script type, make sure you’re using a server script for the billboard GUI or it will only be client-side.

Both scripts are server scripts.

Where is the script located exactly?

The scripts are located in ServerScriptService, the BillboardGUI is in ServerStorage

Did you change NameTag adornee

NameTag.Parent = Head
NameTag.Adornee = Head

There’s a chance characters will load before the .CharacterAdded event fires, causing them to be ignored by the current script. To counter this issues, the script must assume the character may be already loaded:


local NameTagTemplate = template.path.here

local GroupId = 6708836

function PlayerAdded(player)
	local function CharacterAdded(char)
		--adding a time limit of 5, to avoid infinity yield
		local Head = char:WaitForChild("Head", 5) 
		if not Head then warn("Head wasn't found") return end 
		local NameTag = NameTagTemplate:Clone()
		NameTag.Parent = Head

		NameTag.NameText.Text = player.Name
		NameTag.RankText.Text = player:GetRoleInGroup(GroupId)

		char.Humanoid.DisplayDistanceType = Enum.HumanoidDisplayDistanceType.None
	end
	--if it errors the first time, it wont stop the entire script
	pcall(function()
		CharacterAdded(player.Character or player.CharacterAdded:Wait())
	end)
	player.CharacterAdded:Connect(CharacterAdded) 
end

for _, player in pairs(Players:GetPlayers()) do 
	PlayerAdded(player)
end
Players.PlayerAdded:Connect(PlayerAdded)

and to avoid all this messy code, you can take advantage of StarterCharacterScripts which will ensure the script runs when a character loads:

--Script inside StarterCharacterScripts
local Players = game:GetService("Players")

local NameTagTemplate = template.path.here

local GroupId = 6708836

local Character = script.Parent 
local Player = Players:GetPlayerFromCharacter(Character)
if not Player then return end --ignore npcs

local Head = Character:WaitForChild("Head") 
local NameTag = NameTagTemplate:Clone()
NameTag.Parent = Head
NameTag.NameText.Text = Player.Name
NameTag.RankText.Text = Player:GetRoleInGroup(GroupId)

Character.Humanoid.DisplayDistanceType = Enum.HumanoidDisplayDistanceType.None
2 Likes

This script works! Thanks a bunch.

1 Like