Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why isnt this player.Name script working? NOT ANSWERED

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)

2 comments

0

What is this script supposed to do, and what's wrong with it? We need this information to help you get a relevant answer.

0

I fixed it just in case people didnt understand.

2 answers

✓ Accepted answer

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.

1 comment

0

still not working

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

3 comments

0

I just tried this and it doesnt work. Btw The textlabel isnt called lobby. The frame is called that and there are 16 txt labels under that.

0

Are your TextLabels called "Player11", "Player12", "Player13", etc (for players 1-3; ex player 12 would use TextLabel named "Player112"), or "Player1", "Player2", "Player3", etc? (Your script suggests the former, but I guessed the latter). What exactly doesn't work? Is there an error? Also, the "lobby" variable refers to your Lobby frame. This refers to the text label: lobby["Player" .. i]

0

I fixed it to what you said and it still doesnt work. Also How would lobby display text if its not a text label?

This discussion is preserved as it was. New answers, comments, and votes are closed.