How to make textlabel to say username if player doesn't have a display name?

The title is clear & simple, I want my textlabel to show the player’s username If they haven’t set a display name.


Currently, I have my textlabel show this so players may identify another player by seeing both their username & display name.

What my textlabel displays:

DisplayName - (PlayerName)
Or for Example: Thorn - (Niqhtlyy) <— My Display Name & Username.


I have tried my own solutions & looking for solutions via DevForum, Google etc but unfortunately, I had no luck. :frowning_face:

check if the displayName is nil?

https://developer.roblox.com/en-us/api-reference/property/Player/DisplayName

As this article states " * Characters generated with LoadCharacter or by the Roblox engine will have their Humanoid’s DisplayName property assigned to the Player’s DisplayName property."

Basically just check if the DisplayName in the players humanoid is nil or the same as the players name as ComplicatedParadigm said

2 Likes

I think all players have a displayname, and if it isn’t customized it’s their username by default.

Check if displayname is their username to see if they set it.

local function getLabelText(Player)
     local Name = Player.Name
     local Display = Player.DisplayName
     
     return Name ~= Display and Display .. " - (" .. Name .. ")" or Name
end

As far as I’m aware, the DisplayName is automatically set to the Username if someone doesn’t have one.

So I’d follow this advice:

So for example:

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

if Player.DisplayName == Player.Username then
(Your Text Label Location).Text = "@" .. Player.Username
else
(Your Text Label Location).Text = Player.DisplayName
end

I’m not that great at scripting so the code may be wrong