Nametag system doesn't work

So, im trying to make a nametag system for a roblox game, but the problem is that if I join the game using the account whose id matches with the one listed on the table the nametag works properly, but if I used other accounts, the nametag doesnt even show up. Here’s an image:

Here is the script:

game.Players.PlayerAdded:Connect(function(plr) -- Detects a new player that joins
	plr.CharacterAdded:Connect(function(char)
		local Nametag = game.ReplicatedStorage:WaitForChild("NameTag") -- Our nametag

		local clone = game.ReplicatedStorage:WaitForChild("NameTag"):Clone() -- Clones the nametag
		clone.Parent = char.Head -- Put the cloned nametag on the character head
		print("it got cloned")
		local UpperText = char.Head.NameTag.UpperText
		local LowerText = char.Head.NameTag.LowerText		
		
		UpperText.Text = plr.DisplayName.."(@"..plr.Name..")"
		print(UpperText.Text)
		local owners = {1045096972,1226612102} -- Owners list, you can add someone by adding a comma(,)
				
		for i,v in pairs(owners) do -- Checks the table
			if v == plr.UserId then -- Checks if there is a user ID in the table
				LowerText.Text = "Owner" -- The lower text will be owner and everyone will be able to see it
				LowerText.TextColor3 = Color3.new(1, 0.85098, 0) -- Changing the text color to yellow
			end
		end
	end)
end)

Where you’re running the code?
The reason why it applies only for you because you’re probably running the code on the local script.

It’s running in a server script that is placed inside ServerScriptService. Additionally, the nametag only works when I’m playing on an account whose ID matches the one in the table. I can see other people’s nametags, but neither they nor I can see my own nametag.

I took a bit of investigate and here is my solution.
You may need to replicate server change to other clients by making a RemoteEvent and firing it from server to all clients:


So, here is two of me who has joined to the server, i have placed your script to the right location aka ServerScriptService container. When player is being added to the game, the modified code has fired server change to all clients. It actually replicated NameTag to all characters head of clients who was in the server.

Modified Server Script (ServerScriptService):

local remoteEvent = Instance.new("RemoteEvent")
remoteEvent.Name = "NameTagEvent"
remoteEvent.Parent = game.ReplicatedStorage

game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
		local Nametag = game.ReplicatedStorage:WaitForChild("NameTag")

		local clone = Nametag:Clone()
		clone.Parent = char.Head

		local UpperText = clone.Frame.TextLabel
		UpperText.Text = plr.DisplayName .. " (@" .. plr.Name .. ")"

		remoteEvent:FireAllClients(clone, plr)
	end)
end)

Local Script (assigned to PlayerScripts):

local remoteEvent = game.ReplicatedStorage:WaitForChild("NameTagEvent")

remoteEvent.OnClientEvent:Connect(function(clone, plr)
	clone.Parent = plr.Character.Head
end)
1 Like

It says “LocalScript:4: Attempt to index nil with head”

He doesn’t need to do that since action made by server are always replicated !

Alright let me explain the problem once more.
So when im playing the game using an account that matches the ID in the owners table, the nametag works properly, other people can see it, but if I play using an account whose ID does not match with the one in the table, the nametag is not visible at all, neither other player nor I can see the nametag.

1 Like

i am incredibly confused as to why this is happening, the only thing i could think of is that the character is appearing you connect to the character added event.

however there are a few potential problems with the code that could also be causing the problem (and are also far more likely to be causing it), i went ahead and did a quick fix for those:

local Nametag = game.ReplicatedStorage:WaitForChild("NameTag") -- Our nametag
local owners = {1045096972,1226612102} -- Owners list, you can add someone by adding a comma(,)

game.Players.PlayerAdded:Connect(function(plr) -- Detects a new player that joins
	plr.CharacterAdded:Connect(function(char)

		local clone = Nametag:Clone() -- Clones the nametag
		local UpperText = clone:WaitForChild('UpperText')
		local LowerText = clone:WaitForChild('LowerText')
		clone.Parent = char.Head -- Put the cloned nametag on the character head

		UpperText.Text = plr.DisplayName.."(@"..plr.Name..")"

		for i,v in pairs(owners) do -- Checks the table
			if v == plr.UserId then -- Checks if there is a user ID in the table
				LowerText.Text = "Owner" -- The lower text will be owner and everyone will be able to see it
				LowerText.TextColor3 = Color3.new(1, 0.85098, 0) -- Changing the text color to yellow
			end
		end
	end)
end)

if that doesnt work you may want to have the other account reset and see if the nametag appears after they respawn.

what do you mean by “the character is appearing you connect to the character added event”

sorry what i meant to say was:

the only thing i could think of is that the character is appearing before you connect to the character added event.

basically the character added function runs when the player spawns or respawns, so if you connect after the player’s character has loaded it wont run until they respawn again.

Use Plr.CharacterAppearanceLoaded() event instead of Plr.CharacterAdded() very simple fix

1 Like

After a quick test i believe that the problem is infact that the script is connecting to the event to late, because as seen in the video it works perfectly fine in a game without any other scripts. while your game likely has many other scripts which could be delaying the time in which the script gets run.
(this is purely hypothetical)

therefore i’ve made a final edit to the script i previously made, please see if it helps.

local Nametag = game.ReplicatedStorage:WaitForChild("NameTag") -- Our nametag
local owners = {1045096972,1226612102} -- Owners list, you can add someone by adding a comma(,)

local function charloaded(plr, char)
	local clone = Nametag:Clone() -- Clones the nametag
	local UpperText = clone:WaitForChild('UpperText')
	local LowerText = clone:WaitForChild('LowerText')
	clone.Parent = char.Head -- Put the cloned nametag on the character head

	UpperText.Text = plr.DisplayName.."(@"..plr.Name..")"

	for i,v in pairs(owners) do -- Checks the table
		if v == plr.UserId then -- Checks if there is a user ID in the table
			LowerText.Text = "Owner" -- The lower text will be owner and everyone will be able to see it
			LowerText.TextColor3 = Color3.new(1, 0.85098, 0) -- Changing the text color to yellow
		end
	end
end
	
	

game.Players.PlayerAdded:Connect(function(plr) -- Detects a new player that joins
	local Char = plr['Character'] 
	if Char then charloaded(Char, plr) end
	plr.CharacterAdded:Connect(function(char)
		charloaded(plr,char)
	end)
end)
1 Like

I found out that the GUI is correctly parented to the character’s head, the Uppertext and Lowertext text is correct, but for some reason the GUI is just not visible and I dont know why

theres likely another script either disabling the screen gui it or resizing it.

1 Like

So, uh, for some reason, parenting the GUI to the player’s HumanoidRootPart and then setting the Adornee property to the player’s head seems to fix the issue. Anyways, here’s the script if anyone wonders.

game.Players.PlayerAdded:Connect(function(plr) -- Detects a new player that joins
	plr.CharacterAdded:Connect(function(char)
		local Nametag = game.ReplicatedStorage:WaitForChild("NameTag") -- Our nametag


		local clone = game.ReplicatedStorage:WaitForChild("NameTag"):Clone() -- Clones the nametag
		clone.Parent = char:WaitForChild("HumanoidRootPart") -- Put the cloned nametag on the character head
		clone.Adornee = char.Head
		local UpperText = char.HumanoidRootPart.NameTag.UpperText
		local LowerText = char.HumanoidRootPart.NameTag.LowerText		
		
		UpperText.Text = plr.Name -- Makes the upper text the player name
		
		local owners = {1045096972,1226612102} -- Owners list, you can add someone by adding a comma(,)
				
		for i,v in pairs(owners) do -- Checks the table
			if v == plr.UserId then -- Checks if there is a user ID in the table
				LowerText.Text = "Owner" -- The lower text will be owner and everyone will be able to see it
				LowerText.TextColor3 = Color3.new(1, 0.85098, 0) -- Changing the text color to yellow
			end
		end
	end)
end)
1 Like

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