Custom player list only showing local player

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    Player list that shows all players in a game

  2. What is the issue? Include screenshots / videos if possible!
    When I test out my player list, it only shows the name of the local player.
    (Example: Player name is “Player”, it only shows “Player” in the player list even if there is someone else in the game)

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    Putting code in a global script from a local script- didn’t work

Local script:

local players = game:GetService("Players")
local rs = game:GetService("ReplicatedStorage")
local uis = game:GetService("UserInputService")
local ts = game:GetService("TweenService")
local sg = game:GetService("StarterGui")

local plr = players.LocalPlayer
local uid = plr.UserId
local max = players.MaxPlayers

local mainGui = script.Parent
local ui = mainGui.main
local list = ui.list
local presets = ui.presets
local preset = nil

local closeKey = Enum.KeyCode.Tab
local debounce = false
local isOpen = true

local onPos = UDim2.new(0.853, 0 , 0, 0)
local offPos = UDim2.new(1, 0, 0, 0)
local eDir = Enum.EasingDirection.Out
local eSty = Enum.EasingStyle.Quart
local dur = 1
local rep = false

ui.Position = onPos
ui.Visible = true
mainGui.Enabled = true

sg:SetCoreGuiEnabled(Enum.CoreGuiType.PlayerList, false)

if presets:FindFirstChild("preset1") == nil then
	if list:FindFirstChild("preset1") ~= nil then
		preset = list.preset1
		preset.Parent = presets
		preset.Visible = false
	end
else
	preset = presets.preset1
end

if ui:FindFirstChild("1_"..plr.Name) == nil then
	if ui:FindFirstChild(plr.Name) == nil then
		local newCell = preset:Clone()
		newCell.Name = "1_"..plr.Name
		newCell.Parent = list
		newCell.username.Text = "[ @"..plr.Name.." ]"
		newCell.displayName.Text = plr.DisplayName
		newCell.avatar.Image = players:GetUserThumbnailAsync(uid, Enum.ThumbnailType.HeadShot, Enum.ThumbnailSize.Size420x420)
		newCell.Visible = true
	else
		local newCell = ui[plr.Name]
		newCell.Name = "1_"..plr.Name
	end
end

uis.InputBegan:Connect(function(input)
	if input.KeyCode == closeKey then
		if debounce == false then
			debounce = true
			if isOpen == true then
				ui:TweenPosition(
					offPos,
					eDir,
					eSty,
					dur,
					rep
				)
				isOpen = false
			elseif isOpen == false then
				ui:TweenPosition(
					onPos,
					eDir,
					eSty,
					dur,
					rep
				)
				isOpen = true
			end
			task.wait(dur)
			debounce = false
		end
	end
end)

Global script:

local players = game:GetService("Players")

local mainGui = script.Parent
local ui = mainGui.main
local presets = ui.presets
local preset = nil
local list = ui.list

if presets:FindFirstChild("preset1") == nil then
	if list:FindFirstChild("preset1") ~= nil then
		preset = list.preset1
		preset.Parent = presets
		preset.Visible = false
	end
else
	preset = presets.preset1
end

players.PlayerAdded:Connect(function(new)
	local uid = new.UserId
	local newCell = preset:Clone()
	newCell.Name = new.Name
	newCell.Parent = list
	newCell.username.Text = "[ @"..new.Name.." ]"
	newCell.displayName.Text = new.DisplayName
	newCell.avatar.Image = new:GetUserThumbnailAsync(uid, Enum.ThumbnailType.HeadShot, Enum.ThumbnailSize.Size420x420)
	newCell.Visible = true
end)

Here is a screenshot of the layout:
Screenshot (862)

*I know it doesn’t delete the player when they leave, I plan to do that later.

This may be happening because the local script isn’t checking for when players are added to the game. You should probably first begin by creating a function that creates new cells:

local function CreateNewCell(Player)
	local newCell = preset:Clone()
	newCell.Name = tostring(#ui.list:GetChildren())..Player.Name
	newCell.Parent = list
	newCell.username.Text = "[ @"..Player.Name.." ]"
	newCell.displayName.Text = Player.DisplayName
	newCell.avatar.Image = players:GetUserThumbnailAsync(uid, Enum.ThumbnailType.HeadShot, Enum.ThumbnailSize.Size420x420)
	newCell.Visible = true
end

Then you can call this function for all existing players in the game, and connect it to the Players.PlayerAdded event:

for _, Player in ipairs(players:GetPlayers()) do
	CreateNewCell(Player)
end
players.PlayerAdded:Connect(CreateNewCell)
1 Like

Also, in the list instance, did you mean to have a UIListLayout instead of a UIGridLayout?

1 Like