Function flat out doesn't work

Hi! So I have a nametag that I want cloned on the script. For some reason, this function does not work at all:


local bill = game.ReplicatedStorage:WaitForChild("NameTag")

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function()
		if player.Character.Head:FindFirstChild("NameTag") == nil then
			local clone = bill:Clone()
			clone.Enabled = true
			clone.Parent = player.Head
			clone.PlayerName.Text = player.Name
			clone.Title.Text = "Guest"
		end
	end)
end)

This function just doesn’t work for some reason and there are no errors in the output. To work around this, I added a wait function and did the same thing:

wait(3)
	if plr.Character.Head:FindFirstChild("NameTag") == nil then
		print("why")
		local new = nametag:Clone()
		new.Parent = plr.Character.Head
		new.Adornee = plr.Character.Head
		local humanoid = plr.Character:FindFirstChildOfClass('Humanoid')
		humanoid.DisplayDistanceType = "None"
		local uppertext = new.PlayerName
		local lowertext = new.Title
		uppertext.Text = plr.DisplayName
		lowertext.Text = "Guest"
	end

But now I’ve run into in issue where I need the characteradded function to work because if the player resets, the nametag won’t be there. How can I get the characteradded function to work or make it so when the player resets or dies, the nametag is cloned again (another workaround)?

1 Like

Are there errors? Have you tried adding prints to check if CharacterAdded is called and the value of player.Character.Head:FindFirstChild("NameTag")

Just do

if not character.Head:FindFirstChild("NameTag") then

or remove it since when the character gets added, that will only run once, and doesn’t make sense why to have that there.

To try and debug it, i got rid of the if statement and added a print() with this code:

plr.CharacterAdded:Connect(function(char)
		print("starteddddddddd")
		--Varibles
		local head = char.Head
		local newtext = nametag:Clone() --Cloning the text.
		local uppertext = newtext.PlayerName
		local lowertext = newtext.Title
		local humanoid = char.Humanoid

		humanoid.DisplayDistanceType = "None"

		--Main Text
		newtext.Parent = head
		newtext.Adornee = head
		uppertext.Text = plr.DisplayName --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.
		
	end)

The print statement triggers when I join and reset, but nothing happens to the nametag. I don’t know how to fix it.

Look at the post I just made. It tried it and got no luck.

Add a task.wait(1 or what ever number) before if player.Character blah blah blah

1 Like

I have had this issue for the past week and you fixed it! Thank you so much! I didn’t know something as simple as a wait statement was all I needed. Thanks again!

I’ve had this issue too, it’s because when the player’s character is added it isn’t fully loaded, so when it fully loads it deletes the tag

1 Like

Bruh, there’s a character parameter they give you in the player.CharacterAdded function --__–

1 Like

What do you mean???

1 Like

it’s literally the most basic thing. Look up a tutorial on YouTube for player added functions then.

yes, it does return a character parameter but that doesn’t help with the current situation, you still need that wait or it doesn’t work

2 Likes

player.Character doesn’t work at all when you do this…

They are correct, the ‘CharacterAdded’ event/signal fires whenever the character model is added, not when it has loaded, to correctly wait for a character’s appearance to load listen for the ‘CharacterAppearanceLoaded’ event/signal to fire.

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

local function OnPlayerAdded(Player)
	local function OnCharacterAdded(Character)
		if not Player:HasAppearanceLoaded() then Player.CharacterAppearanceLoaded:Wait() end
		local Head = Character:FindFirstChild("Head") or Character:WaitForChild("Head")
		local BillboardGui = Instance.new("BillboardGui")
		BillboardGui.Adornee = Head
		BillboardGui.Parent = Head
	end

	Player.CharacterAdded:Connect(OnCharacterAdded)
end

Players.PlayerAdded:Connect(OnPlayerAdded)
1 Like