Custom Playerlist works but doesnt add the current players in the server to the new player's playerlist

Hello developers!

  1. What do you want to achieve? I want to make a custom player list.

  2. What is the issue? New players to the server dont see the players that were there before them and still in the game in the player list.

  3. What solutions have you tried so far? I tried making it so the new player does get the new players but it doesnt work.

It is in a Normal script in ServerScriptService. Here is my script

game.Players.PlayerAdded:Connect(function(newplayer)
	wait(2)
	for i, player in pairs(game.Players:GetPlayers()) do
		script.List:Clone().Parent = player.PlayerGui.NewPlayerList.ScrollingFrame
		player.PlayerGui.NewPlayerList.ScrollingFrame.List.Name = newplayer.Name
		player.PlayerGui.NewPlayerList.ScrollingFrame[newplayer.Name].PlayerName.Text = newplayer.Name
		player.PlayerGui.NewPlayerList.ScrollingFrame[newplayer.Name].Visible = true
		script.List:Clone().Parent = newplayer.PlayerGui.NewPlayerList.ScrollingFrame
		newplayer.PlayerGui.NewPlayerList.ScrollingFrame.List.PlayerName.Text = player.Name
		newplayer.PlayerGui.NewPlayerList.ScrollingFrame.List.Name = player.Name
		if newplayer:IsInGroup(901313) then
			player.PlayerGui.NewPlayerList.ScrollingFrame[newplayer.Name].PlayerName.Roundify.ImageColor3 = Color3.new(0,0,255)
		end
	end
end)

Please just tell me why it wont add preexisting players for the new player. Also Im very sorry if the issue is very clear. I’m not that skilled with doing this.

1 Like

You’ll need to create a remote event that sends the new data using :FireAllClients()

Your function is inside an event that only gets called when a new player is added. If a newer player joins, those players that didn’t have a player list will now be able to see the player list, but the newest player will not.

To fix this, simply call the function once outside of the event function:

function refreshPlayerList()
	-- where you update the playerlist
end

refreshPlayerList()

game.Players.PlayerAdded:Connect(function(newplayer)
	wait(2)
	refreshPlayerList()
end)

While writing the above code, I noticed you have a deeper issue. This code seems to be run on the serverside. You should never interact with GUIs on the server. The client should have a local script to handle any gui stuff you need to get done.

Once you do so, your code becomes simple. You only have to worry about the client’s look of the player list rather than every client on the server.

Another issue is that you only add new characters. The simplest way to fix it is to delete everything inside of the scrolling from and reconstruct the entire list using a for loop that is iterating through the players.

Edit: Here’s how it might look

local Player = game.Players.LocalPlayer
local ScreenGui = script.Parent -- put this local script inside of the screengui
local Container = ScreenGui.ScrollingFrame

local ListItem = script.List

function refreshPlayerList()
	Container:ClearAllChildren()
	for _, player in pairs(game.Players:GetPlayers()) do
		local g = ListItem:Clone()
		g.PlayerName.Text = player.Name
		g.Visible = true
		g.Parent = Container
		if (player:IsInGroup(901313) then
			-- note that color3's range value is from 0 to 1, not 0 to 255
			g.PlayerName.Roundify.ImageColor3 = Color3.new(0,0,1)
		end
	end
end

refreshPlayerList()

game.Players.PlayerAdded:Connect(function(newplayer)
	wait(2)
	refreshPlayerList()
end)

You can make it more efficient by adding only new players and removing players who aren’t there, but that’s the jist of it.

I am going to try this. I will let you know if it works.