Need help with my script that shows every player's name in a GUI

I need help with my script that I made. My script should enable to see all the player’s names in a gui listed under each other.

However there is an issue. When I run a server test with 3 players, It only shows the name of player 1 and player 3 in the gui instead of player 1, player 2 and player 3. I have done research related to this topic but could not find any helpful information.

Here is the script. Thanks in advance!

local mainFrame = script.Parent:WaitForChild("MainFrame")
local sample = script.Parent.MainFrame:WaitForChild("Sample")

local function addPlayers()
	local players = game.Players:GetPlayers() --getting the players in game
	for i, v in pairs(players) do --looping to the players and adding a textbutton with the players name.
		
		local clonedSample = sample:Clone()
		clonedSample.Name = v.Name
		clonedSample.Text = v.Name
		clonedSample.Visible = true
		clonedSample.Parent = mainFrame
		table.remove(players,i) -- Removing the player from the table, so it does not add unlimited buttons with the player's name. I think this is the problem.
	end
end

local function update()
	
	for _, v in pairs(mainFrame:GetChildren()) do --removing the old buttons
		if v.Name ~= "Sample" and v.Name ~= "PlayersText" and v.Name ~="UIListLayout" then
			v:Destroy()
			print("Old frames destroyed")
			
		end
	end
end

while true do wait()
	wait(4)
	addPlayers()
	wait(4)
	update()
end


3 Likes

So if I understand this, you’re making some sort of playerlist in a GUI to show everyone’s name?

1 Like

No, just a gui that shows every players name. See the picture above. It should list the following:

  • Player 1
  • player 2
  • player3

Alright, you could make it so whenever the server detects a new player has been added to the server it adds a new button with the players name to the button.

You could use a CreatePlayerEntry().

local function CreatePlayerEntry(player)

Correct me if I’m wrong, you mean like create a script with

`lua
game.Players.PlayerAdded:Connect(function()

end)
`

Yeah, that’s right. You would need to make a function that makes the servers add a label with the player’s name on joining. Make a UiLayout so I stays in line and nothing gets placed on a random position.

Creating a player list is actually more simple than it seems. Here are 3 things to know:

  1. When you join, loop through all the players and create a frame for each one
  2. When someone else joins, use a PlayerAdded event to add a player to the list
  3. When a player leaves, use the PlayerRemoving event to remove a player from the player list
--// Local Script //--

local Players = game:GetService("Players")

local function Create(player)

end

local function Remove(player)

end

for i, player in pairs(Players:GetPlayers()) do
    Create(player)
end

Players.PlayerAdded:Connect(function(player)
    Create(player)
end)

Players.PlayerRemoving:Connect(function(player)
    Remove(player)
end)

Are there any errors in your output because this script looks like it should work.

The only thing that looks questionable is your table.remove part of your script. I would also use a playeradded event and update you GUI every time a player joins or leaves. Then table.remove wouldn’t be necessary.

No there are no errors given. 30 char 3- char 30 char 3- char30 char 3- char30 char 3- char30 char 3- char30 char 3- char30 char 3- char30 char 3- char30 char 3- char30 char 3- char30 char 3- char

Mind me asking, what is the point of spamming 30 chars?

Made this script. No errors are given. Nothing is happening. I dont think you can use Players.PlayerAdded:Connect(function(player) in a local script. Heres the script


local mainFrame = script.Parent:WaitForChild("MainFrame")
local sample = script.Parent.MainFrame:WaitForChild("Sample")



local players = game:GetService("Players")

for i, v in pairs(players:GetPlayers()) do
	local clonedSample = sample:Clone()
	clonedSample.Name = v.Name
	clonedSample.Text = v.Name
	clonedSample.Parent = mainFrame 
end

players.PlayerAdded:Connect(function(player)
	local clonedSample = sample:Clone()
	clonedSample.Name = player.Name
	clonedSample.Text = player.Name
	clonedSample.Parent = mainFrame 
end)

It is a 30 character minimum, not a 30 word minimum haha!

My bad. I’m new on this forum lol.

1 Like

I agree that you cannot use playeradded in a localscript. You need to do it in the server and pass over the player who joined to a localscript with a remote event.

I used the table.remove so that it does not add infinite player buttons

I get what you are using it for but I don’t think it is necessary if you update your table every time a player joins.

It will save memory that way also and reduce lag.

Would you mind help me a little bit with the remote event part. I’m not really good with remote events. How do I make it once a player joins (in a server script) a remote event gets fired to a local script with the name of the player

Alright. I can’t really help you any more because I have said what I have to say. I would really take a playeradded approach to this because it is good practice to not have while true do loops running all the time. Good luck @InvalidArgvmen!

Hey, I’m back sorry for delay I was making a post.
PlayerAdded and a PlayerRemoved;

local Players = game:GetService("Players")
 
Players.PlayerAdded:Connect(function()
	-- Adding joined players.
end)
 
Players.PlayerRemoving:Connect(function()
	--Removing the players.
end)