Issue with DisplayName

l’m having trouble writing this basic code which simply clones a BillboardGui into the players head. And I want a Text child inside of the BillBoardGui to have its text containing the player’s DisplayName.

You can imagine it as a custom nametag GUI for the player which displays above the player’s head.

I receive these errors:

A) If it’s a LocalScript, I get:

Head is not a valid member of Players.MyPlayerNameHere

B) If it’s a server script, I get:

Workspace.Script:6: attempt to index nil with 'DisplayName'

local Players = game:GetService("Players")
local player = game.Players.LocalPlayer

game.Workspace.ChildAdded:connect(function() 
	game.Lighting.NameTag.Text.TextStrokeTransparency = 0
	game.Lighting.NameTag.Text.Text = "[🇹🇷] " .. player.DisplayName .. ""
	game.Lighting.NameTag:Clone().Parent = player.Head

end)

What have I written wrong or what am I missing?

If you want the BillboardGui to be visible by every player, you have to do this in a ServerScript :

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player) -- Detect if a player joins

player.CharacterAppearanceLoaded:Connect(function() -- Wait for the character of the player to load but maybe you don't need this

	game.Lighting.NameTag.Text.TextStrokeTransparency = 0
	game.Lighting.NameTag.Text.Text = "[🇹🇷] " .. player.DisplayName .. ""
	game.Lighting.NameTag:Clone().Parent = player.Character.Head -- Put the Gui inside the Head of the player's character

end)

Normally, it should work if you do this, the problem you encounter is that you try to put the Gui inside the player and not inside his character, the place where you try to put is that :
image

And not the character model, now if you want the BillboardGui only visible by the actual player, put this script inside a LocalScript :

local Players = game:GetService("Players")
local player = game.Players.LocalPlayer

player.CharacterAppearanceLoaded:Connect(function() -- Wait for the character of the player to load but maybe you don't need this

	game.Lighting.NameTag.Text.TextStrokeTransparency = 0
	game.Lighting.NameTag.Text.Text = "[🇹🇷] " .. player.DisplayName .. ""
	game.Lighting.NameTag:Clone().Parent = player.Character.Head -- Put the Gui inside the Head of the player's character

end)

It’s almost the same but not exactly, this script only make it visible for the actual player

If it don’t do what you wanted, just let me know.

1 Like

I decided to use the server script code, it worked out, thanks so much for the detailed explanation and codes, love it.

1 Like

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