Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to prevent usernames on Playerlist / Leaderboard GUI from duplicating?

I've been searching for quite a while for an answer to this question, and I've only ever found one thing related to it anywhere on this website or on roblox devforum.

I've created a custom leaderboard UI, with two sections on it, each corresponding to a team. When there is one player in the server, it displays the player's name on the correct side of the GUI - the side corresponding to the player's team. However, if you go to 2 or more players, for some reason it duplicates and shows players on both sides, no matter what, as shown below:

https://imgur.com/a/q8ljUgZ

This is the part of the script dedicated solely to handling the playerlist:

function fill()
	wait(.5)
	clear()
	for _, player in pairs(game.Players:GetChildren()) do
		for i,v in pairs(game.Players:GetChildren()) do
			if v.Team == game.Teams["Team A Dead"] or v.Team == game.Teams["Team A"] then
				print(player.Name .." is on Blue Team")
				local new = template:Clone()
				new.Name = player.Name
				new.Username.Text = player.Name
				new.Parent = plistblue
			elseif v.Team == game.Teams["Team B Dead"] or v.Team == game.Teams["Team B"] then
				print(player.Name .." is on Red Team")
				local new2 = template:Clone()
				new2.Name = player.Name
				new2.Username.Text = player.Name
				new2.Parent = plistred
			end
		end
	end
end

while wait(0.5) do
	fill()
end

Any sort of help would be appreciated. I feel like I've probably made a minor mistake here, however I am very very new to scripting and rely pretty heavily on seeing other people's work to understand how to make my own scripts, and this is an area where there are not a lot of available resources.

Thanks in advance.

1 answer

I think you're going about this the wrong way. You should be using events such as :PlayerAdded() or :GetPropertyChangedSignal() to update the player list instead of updating it every tick/second/minute - which is more intensive.

Here's a simpler way to populate your player list and update it:

function createPlayerOnPlayerList(player)
	local clone = template:Clone()
	clone.Name = player.Name
	clone.Parent = -- whatever it should be
	-- checks if a player changes teams
	player:GetPropertyChangedSignal("Team"):Connect(function()
		clone.Parent = -- whatever it should be
	end)
end)

for _,player in pairs(Players:GetPlayers()) do
	createPlayerOnPlayerList(player)
end
Players.PlayerAdded:Connect(function(player)
	createPlayerOnPlayerList(player)
end)
Players.PlayerRemoving:Connect(function(player)
	PlayerList:FindFirstChild(player.Name, true):Destroy()
end)

It is important you always check the API documentation for objects and look for events/functions that will make your life easier.

This discussion is preserved as it was. New answers, comments, and votes are closed.