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.
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.
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)