Multiple PlayerList buttons

Hello,

I am trying to make a leaderboard where if you click someones name, then a tab appears giving some more information about that player. However, I have ran into a wall where I do not know how to tell which frame the user has clicked. Each player has their own separate frame named with their own player name. So, how can I determine if a player clicks my frame as opposed to yours?

I know I could probably just number each frame and call that one when it’s clicked, but there must be an easier way of checking all of the buttons and which one was clicked.

Any help is greatly appreciated.

Best,
Surgo

You can create an array of all the frames and hook an event to each one.

Here would be an example:

local PlayerListFrames = script.PlayerListFrames:GetChildren()

for _,v in pairs(PlayerListFrames) do
    v.MouseButton1Click:Connect(function()
       print(v.TextLabel.Text) --> JohnDoe1234 
    end)
end

This, of course, is assuming there is a TextLabel inside the frame which contains the player’s name as the Text property. You would need to modify this to suit your specific situation but I hope it helps.

1 Like

How do I update this list as players join and leave the game? Would I create a function and recall the getchildren? Then, I call this new function inside the PlayerAdded and PlayerRemoved function?

When a new frame is added to the GUI, you can hook the event to it.

script.PlayerListFrames.ChildAdded:connect(function(newframe)
    newframe.MouseButton1Click:Connect(function()
       print(newframe.TextLabel.Text) --> NewPlayer1236
    end)
end)

Would there be any reason as to why the code above will only work for every other button? Is that something from above, or could I have done something?

function playerManager(player, goal)
	if goal == "add" then
		local newplayer = baseFrame:Clone()
		newplayer.Parent = BoardFrame
		newplayer.Name = player.Name
		newplayer.Visible = true
		newplayer.TextLabel.Text = player.Name
		local headshot = Players:GetUserThumbnailAsync(player.UserId, Enum.ThumbnailType.HeadShot, Enum.ThumbnailSize.Size420x420)
		newplayer.ImageLabel.Image = headshot
		
		for _,v in pairs(BoardFrame:GetChildren()) do
			if v:IsA("Frame") then
			    v.TextButton.MouseButton1Click:Connect(function()
					toggleUser(v, true)
			    end)
			end
		end

	else
		if BoardFrame:FindFirstChild(player.Name) then
			BoardFrame:FindFirstChild(player.Name):Destroy()
		else
			warn(player, "does not exist in list")
		end
	end
end

There’s more code, but I don’t think it would be anywhere else except this function.

Well, actually here is the block of code where I show the player info frame:

local current = nil
    function toggleUser(player, toggle)
    	if current == player or not toggle then
    		current = nil
    		PlayerStats.Visible = false
    	else
    		current = player
    		PlayerStats.Visible = true
    		local plr = game.Players:FindFirstChild(player.Name)
    		if plr then
    			local headshot = Players:GetUserThumbnailAsync(plr.UserId, Enum.ThumbnailType.HeadShot, Enum.ThumbnailSize.Size420x420)
    			PlayerStats.ImageLabel.Image = headshot
    		end
    		PlayerStats.UserName.Text = player.Name
    		PlayerStats.Team.Text = game.Players:FindFirstChild(player.Name).Team.Name
    	end
    end

I’m unsure where many of these variables are coming from and what the values of your parameters are being set to, mind showing your full code?

local Players = game:GetService("Players")
local UserInputService = game:GetService("UserInputService")

Players.LocalPlayer:WaitForChild("PlayerGui"):SetTopbarTransparency(1)
game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.PlayerList, false)
game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Health, false)

local leaderboard = script.Parent:WaitForChild("Leaderboard")
local PlayerStats = leaderboard:WaitForChild("PlayerStats")
local BoardFrame = leaderboard:WaitForChild("BoardFrame")
local baseFrame = BoardFrame:WaitForChild("baseFrame")

local current = nil

function toggleUser(player, toggle)
	if current == player or not toggle then
		current = nil
		PlayerStats.Visible = false
	else
		current = player
		PlayerStats.Visible = true
		local plr = game.Players:FindFirstChild(player.Name)
		if plr then
			local headshot = Players:GetUserThumbnailAsync(plr.UserId, Enum.ThumbnailType.HeadShot, Enum.ThumbnailSize.Size420x420)
			PlayerStats.ImageLabel.Image = headshot
		end
		PlayerStats.UserName.Text = player.Name
		PlayerStats.Team.Text = game.Players:FindFirstChild(player.Name).Team.Name
	end
end

for _,v in pairs(BoardFrame:GetChildren()) do
	if v:IsA("Frame") then
	    v.TextButton.MouseButton1Click:Connect(function()
			print("Is this ever used? Yes. Yes it is")
			toggleUser(v, true)
	    end)
	end
end

function playerManager(player, goal)
	if goal == "add" then
		local newplayer = baseFrame:Clone()
		newplayer.Parent = BoardFrame
		newplayer.Name = player.Name
		newplayer.Visible = true
		newplayer.TextLabel.Text = player.Name
		local headshot = Players:GetUserThumbnailAsync(player.UserId, Enum.ThumbnailType.HeadShot, Enum.ThumbnailSize.Size420x420)
		newplayer.ImageLabel.Image = headshot
		
		for _,v in pairs(BoardFrame:GetChildren()) do
			if v:IsA("Frame") then
			    v.TextButton.MouseButton1Click:Connect(function()
					toggleUser(v, true)
			    end)
			end
		end
		
	else
		if BoardFrame:FindFirstChild(player.Name) then
			BoardFrame:FindFirstChild(player.Name):Destroy()
		else
			warn(player, "does not exist in list")
		end
	end
end


for _,v in pairs(Players:GetChildren()) do
	playerManager(v, "add")
end

Players.PlayerAdded:connect(function(player)
	playerManager(player, "add")
end)

Players.PlayerRemoving:connect(function(player)
	playerManager(player, "remove")
end)

leaderboard.MouseEnter:connect(function()
	leaderboard:TweenPosition(UDim2.new(0.65, 0, 0, 0), "Out", "Quart", 1, true)
end)

leaderboard.MouseLeave:connect(function()
	leaderboard:TweenPosition(UDim2.new(0.65, 0, -0.45, 0), "Out", "Quart", 1, true)
	toggleUser(nil, false)
end)