Complications giving players name card/getting character

I’m attempting to give players a custom name card above their heads containing their display name and username. The problem is that I always end up with this error.

attempt to index nil with ‘WaitForChild’

I’m not quite sure how to resolve this error, I’ve tried waiting for the character to load, but idk if that’s the issue either.

(the script is inside of ServerScriptService)

game.Players.PlayerAdded:Connect(function(player)
	
local char = player.Character
local repStorage = game:GetService("ReplicatedStorage")
local nameCardClone = repStorage.nameCard:Clone()

	game:IsLoaded()
	
	nameCardClone.Parent = player.PlayerGui
	
	player.PlayerGui.nameCard.Adornee = char:WaitForChild("Head")
	
player.PlayerGui:WaitForChild("nameCard").nameFrame.nameLabel.Text = player.DisplayName	
player.PlayerGui:WaitForChild("nameCard").usernameFrame.nameLabel.Text = player.Name

end)

I will provide extra information if necessary.
Thanks in advance.

I think this is happening because you can’t access the player’s PlayerGui from the server, since you said you’ve tried waiting for the character.

I’ve tried multiple ways of doing this, like moving the billboard GUI into the player’s head, but I always get the same error.

On which line of code is the error occurring? This one?

player.PlayerGui.nameCard.Adornee = char:WaitForChild(“Head”)

Also, you should wrap the code inside of the Players.PlayerAdded event in a Player.CharacterAdded event. If you don’t do this, the name tag will only appear once; and the fact that you’re not wrapping your code in a Player.CharacterAdded event may be the reason why your script is not working.

Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(character)
        -- Add name tag here.
    end)
end)

If the error is occurring at the line I quoted, the reason this is happening probably is the reason I provided – the char is nil because the character wasn’t added.

1 Like

Then just don’t use wait for child you already established the namcardclone why are you going through all the decendants to get to it again.
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(char)
local repStorage = game:GetService(“ReplicatedStorage”)
local nameCardClone = repStorage.nameCard:Clone()
game:IsLoaded()
nameCardClone.Adornee = char:WaitForChild(“Head”)
nameCardClone.usernameFrame.nameLabel.Text = player.Name
nameCardClone.Parent = player.PlayerGui
end)
end)

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