How to create a text label with player name?

Hello developers! I have recently hit an issue when creating an update for a server. What I am trying to do is create a text label to when the player joins they see their name, and when the other player joins he sees his name. Or to simplify, this is what I am trying to make.

  • 2 Players are in the server, Player A and Player B
  • When Player A is playing his name is on the label
  • When Player B is playing at the same time, Player B sees his name, Player B

Issue

Currently, I have the text label created, but the issue is getting it to work.

Tried Solutions

Currently, I have tried some other tutorials since I have never done this kind of work. I have this current script inside the label as a local script.

local player = game.Players.LocalPlayer
local name = player.Name
game.StarterGui.Playername.Plabel.Text = player.Name

I do not know if this is the right script or not, but so far it is not working. Here is a picture of the label, to show an example of what I have.

Screen Shot 2022-01-06 at 11.54.02 AM 2

When the player joins, “Label” is replaced by his name.

Am I at least on the right track for my goal?

1 Like

“StarterGui” is not the player’s “current” gui. Those gui elements are COPIED to the player when the player starts the game. Changing those values via script will not impact the current player.

Here’s a link to the Developer doc on getting the player’s current Gui:

https://developer.roblox.com/en-us/api-reference/class/PlayerGui

Without seeing all of your code and elements, you’re likely looking to do something more like this:

local player = game.Players.LocalPlayer
local name = player.Name
local playerGui = player:WaitForChild('PlayerGui')
playerGui.Plabel.Text = name

Hope this helps!

3 Likes

Hello, sorry it took so long to get back… I tested it and it did not work. But this did help because I took a bit of one tutorial and then put that with this and it worked! ty dude!


2 Likes

If you have issues locating the PlayerGui, you can just place the LocalScript inside Plabel and use script.Parent:

local Plabel = script.Parent 
Plabel.Text = game.Players.LocalPlayer.Name
2 Likes

that script didn’t work for me