I made a script yesterday and today I fixed up so it should work but its still doesn't. I want to have the player's name show up in a textlabel. I have 16 players (max) in the game so that would mean 16 textlabels. Would I have to put this script inside each one (of course the name of the txtlabel would be different)?
To sum up : How do I get the player's name to show and Would I put this into 16 different txt labels?
script :
game.Players.PlayerAdded:connect(function(player) for _, player in pairs(game.Players:GetPlayers()) do game.StarterGui.MainGui.MenuButton.Lobby["Player1" .. game.Players.NumPlayers].Text = player.Name end end)
This is a common mistake. You are changing the GUI inside StarterGui, you want to change the GUI inside of the playerGui (I hope).
game.Players.PlayerAdded:connect(function(player) for _, player in pairs(game.Players:GetPlayers()) do player.PlayerGui:WaitForChild("MainGui"):WaitForChild("MenuButton"):WaitForChild("Lobby")["Player1" .. game.Players.NumPlayers].Text = player.Name end end)
I have added plenty of WaitForChild()s, as well. If you are using FE, then this will be completely different.
There are some problems with your design. ex What happens if 2+ players leave, followed by someone else joining? You will potentially overwrite the TextLabel for someone who is still in the game.
Instead, do this:
local lobby = game.StarterGui.MainGui.MenuButton.Lobby game.Players.PlayerAdded:connect(function(player) local ch = game.Players:GetPlayers() for i = 1, #ch do lobby["Player" .. i].Text = ch[i].Name end for i = #ch + 1, 16 do --clear the unused TextLabels lobby["Player" .. i].Text = "" end end)
Now each label is guaranteed to hold the name of a player in the game (or be an empty string).