Making an interactable playerlist

Hiya everyone. I am currently trying to make an interactable playerlist so you just click on the player on the playerlist and information is displayed about them but I’ve ran into 2 problems.

1, my function for it doesn’t work through the current player list ( basically, the playerlist works fine but before in my script i’ve made a function where it gathers all of the textbuttons in the GUI, when they are clicked individually they make a noise but that doesn’t seem to work with the buttons in the playerlist) so my function to get information about the player that is clicked is ignored.

2, When I remove the playerlist function and add a normal button into the scrollingframe it all works fine, but when I press the button I have set it so it prints the players name but when it prints it comes back as “{…}”. Sorry for the lengthy paragraph.

here is my script for reference

for _,FindPlayer in pairs(Playerlist.ScrollingFrame:GetChildren()) do
	if FindPlayer:IsA("TextButton") then
		FindPlayer.MouseButton1Click:Connect(function(FoundPlayer)
			FoundPlayer = Players:GetPlayers(FindPlayer.Text)
			print(FoundPlayer)
		end)
	end
end


function RemovePlayer()
	for _,FindPlayerWhenLeft in pairs(Playerlist.ScrollingFrame:GetChildren()) do
		if FindPlayerWhenLeft.Name == "User" then
			FindPlayerWhenLeft:Destroy()
		end
	end
end
function AddNewPlayer()
	for _,Player in pairs(Players:GetPlayers()) do
		local NewPlayer = Templates.User:Clone()
		NewPlayer.Visible = true
		NewPlayer.Text = Player.Name
		NewPlayer.Parent = Playerlist.ScrollingFrame
	end
end
Players.PlayerAdded:Connect(AddNewPlayer)
Players.PlayerRemoving:Connect(RemovePlayer)
AddNewPlayer()
-- create the ScreenGui and add it to the Players object
local screenGui = Instance.new("ScreenGui")
screenGui.Name = "PlayerList"
screenGui.Parent = game.Players

-- create the ScrollingFrame and add it to the ScreenGui
local scrollingFrame = Instance.new("ScrollingFrame")
scrollingFrame.Name = "List"
scrollingFrame.Parent = screenGui

-- create a table to store the player buttons
local playerButtons = {}

-- create a function to add a player to the list
local function addPlayer(player)
  -- create a new frame for the player
  local frame = Instance.new("Frame")
  frame.Name = player.Name
  frame.Parent = scrollingFrame

  -- create a new TextButton for the player
  local button = Instance.new("TextButton")
  button.Name = "Button"
  button.Text = player.Name
  button.Parent = frame

  -- add the button to the playerButtons table
  playerButtons[player] = button
end

-- create a function to remove a player from the list
local function removePlayer(player)
  -- get the player's button from the playerButtons table
  local button = playerButtons[player]

  -- destroy the button and remove it from the playerButtons table
  button:Destroy()
  playerButtons[player] = nil
end

-- add all existing players to the list
for _, player in pairs(game.Players:GetPlayers()) do
  addPlayer(player)
end

-- connect to the PlayerAdded and PlayerRemoving events
game.Players.PlayerAdded:Connect(addPlayer)
game.Players.PlayerRemoving:Connect(removePlayer)```

Thank you so much for this I really appreciate it.

Hi again. Sorry if this is annoying but after testing this I’ve found that the buttons don’t work if it was added after you joined so you have to refresh yourself for the buttons to work again. Is there a work around for this?

Yes, you could manually update the playerlist. So just take the default playerlist, when someone joins you just copy it into their playerGui and delete the wrong one

Would I need to use a remote event for that? because it needs to be refreshed for everyone

You could do that, yes. Listen on the client for an update event. On the server you just fire the remote to all clients.

yeah; that was what I was planning on doing.

Alternatively, you could use Fusion for this. That can automatically update everything for you.

I actually don’t know what that is

https://elttob.uk/fusion-new-website/

It’s a library for creating UI.

Sorry this is probably a stupid question but does it do it automatically or…

No, you’d need to learn how to use Fusion:
Here’s a tutorial on a playerlist

I think i’ll stick with remote events. Also sorry if im being a total utter pain but It’s still not working. I still have to refresh myself in order for it to work; I’ll send my script.

Client

local playerButtons = {}
local function addPlayer(Player)
	local PlayerJoined = Templates.User:Clone()
	PlayerJoined.Text = Player.Name
	PlayerJoined.Name = Player.Name
	PlayerJoined.Visible = true
	PlayerJoined.Parent = StaffHandler.Playerlist.ScrollingFrame
	playerButtons[Player] = PlayerJoined
	local Playerlist = StaffHandler.Playerlist
	local Table = {Playerlist}
	Events.Staff:FireServer("UpdatePlayerlist")
end
local function removePlayer(Player)
	local PlayerJoined = playerButtons[Player]
	PlayerJoined:Destroy()
	playerButtons[Player] = nil
end

for _,Player in pairs(game.Players:GetPlayers()) do
	addPlayer(Player)
end
Players.PlayerAdded:Connect(addPlayer)
Players.PlayerRemoving:Connect(removePlayer)

wait(1)
Events.Staff.OnClientEvent:Connect(function(Action)
	if Action == "UpdatedPlayerlist" then
		for _,Player in pairs(Players:GetPlayers()) do
			if Player:GetRankInGroup(ServerGroupID) > MinimumStaffRank then
				local NewPlayerlist = Playerlist:Clone()
				NewPlayerlist.Parent = Player.PlayerGui:WaitForChild("StaffAccessUI"):WaitForChild("StaffHolder")
				Playerlist:Destroy()
			end
		end
	end
end)

Server

Events.Staff.OnServerEvent:Connect(function(Player, Action, Table)
	if Action == "UpdatePlayerlist" then
		Events.Staff:FireAllClients("UpdatedPlayerlist")
	end
end)