My custom player list is creating duplicates

I currently need help fixing a problem with my custom player list.
The problem is, it is creating duplicates.
I test my player list by a local server > 8 players.
When I did that, it gave me about 5 duplicates or more duplicates.


How am I replicating Roblox’s player list:
I link the local script to a remote which is fired by a server script when a player joins, so that it wouldn’t rely on internal functions.
The server script is doing FireAllClients so that remaining players in the server will have their leaderboard updated as well.
But since players join at different times, they may not see other players in the player list.
So, I made it so it would gather all players, and if they do not exist in the list, it will create one for them.
My Local script:

game:GetService('StarterGui'):SetCoreGuiEnabled(Enum.CoreGuiType.PlayerList, false)
game.ReplicatedStorage.Leaderboard.playerinfo.OnClientEvent:Connect(function(player,tags,rating,bool)
	if bool == true then
		local label = script.Parent.ec.user:Clone()
		label.Parent = script.Parent.bg
		label.Text = player.Name
		label.rate.Text = 'Ethnicity: '..rating
		label.tags.Text = tags
		if label.tags.Text == nil or label.tags.Text == '' then
			-- nothing
		else
			label.tags.Visible = true
		end
		label.Name = player.Name
		label.Visible = true
		for _,_p in pairs(game.Players:GetChildren()) do
			if script.Parent.bg:FindFirstChild(_p.Name) then
				if script.Parent.bg:FindFirstChild(_p.Name).Visible == false then
					script.Parent.bg:FindFirstChild(_p.Name).Visible = true
				end
			else
				if not _p:FindFirstChild('leaderstats') then
				wait(1)
                local v = script.Parent.ec.user:Clone()
				v.Parent = script.Parent.bg
				v.Text = _p.Name
				v.rate.Text =  'Ethnicity: '.._p['leaderstats']['SuccessRate'].Value
				v.tags.Text = _p['leaderstats']['tags'].Value
				if v.tags.Text == nil or v.tags.Text == '' then
			        -- nothing
		        else
			        v.tags.Visible = true
		        end
				v.Name = _p.Name
				v.Visible = true
				else
				local v = script.Parent.ec.user:Clone()
				v.Parent = script.Parent.bg
				v.Text = _p.Name
				v.rate.Text =  'Ethnicity: '.._p['leaderstats']['SuccessRate'].Value
				v.tags.Text = _p['leaderstats']['tags'].Value
				if v.tags.Text == nil or v.tags.Text == '' then
			        -- nothing
		        else
			        v.tags.Visible = true
		        end
				v.Name = _p.Name
				v.Visible = true
				end
			end
		end
		for _,_b in pairs(game.Players:GetPlayers()) do
			if not script.Parent.bg:FindFirstChild(_b.Name) then
				local v = script.Parent.ec.user:Clone()
				v.Parent = script.Parent.bg
				v.Text = _b.Name
				v.rate.Text = 'Ethnicity: '.._b['leaderstats']['SuccessRate'].Value
				v.tags.Text = _b['leaderstats']['tags'].Value
				if v.tags.Text == nil or v.tags.Text == '' then
					-- nothing
				else
					v.tags.Visible = true
				end
				v.Name = _b.Name
				v.Visible = true
			end
		end
	elseif bool == false then
		if script.Parent.bg:FindFirstChild(player.Name) then
			script.Parent.bg:FindFirstChild(player.Name):Destroy()
		end
	end
end)
game.ReplicatedStorage.Leaderboard.ratechange.OnClientEvent:Connect(function(p,val)
	if script.Parent.bg:FindFirstChild(p.Name) then
		script.Parent.bg:FindFirstChild(p.Name)['rate'].Text = 'Ethnicity: '..val
	end
end)

My server script:

local pi = game.ReplicatedStorage.Leaderboard.playerinfo
game.Players.PlayerAdded:Connect(function(p)
	if not p:FindFirstChild('leaderstats') then
		repeat
			wait()
		until p:FindFirstChild('leaderstats')
		if p.Name == 'Obj_ective' then
			p['leaderstats']['tags'].Value = 'Creator'
		else
			if p.Name == 'Player1' then
				p['leaderstats']['tags'].Value = 'Tester'
			end
		end
		if p['leaderstats']['SuccessRate'].Value == 0 then
			repeat
				wait()
			until p['leaderstats']['SuccessRate'].Value ~= 0
		end
		pi:FireAllClients(p,p['leaderstats']['tags'].Value,p['leaderstats']['SuccessRate'].Value,true)
		p['leaderstats']['SuccessRate'].Changed:connect(function()
			game.ReplicatedStorage.Leaderboard.ratechange:FireAllClients(p,p['leaderstats']['SuccessRate'].Value)
		end)
	end
end)
game.Players.PlayerRemoving:Connect(function(p)
	pi:FireAllClients(p,0,0,false)
end)

Note: I looked everywhere and nothing was found to solve this issue of mine.
I removed all repeats(I put repeats in the local script to check if they existed in the list).
But, I still don’t have my solution.
I am taking suggestions and solutions.

You could check if there’s already a frame/text, something like this:

if not script.Parent.bg:FindFirstChild(player.Name) then
    local label = script.Parent.ec.user:Clone()
    label.Parent = script.Parent.bg
    label.Text = player.Name
    label.rate.Text = 'Ethnicity: '..rating
    label.tags.Text = tags
    ...your code continues...
end

It would prevent duplicates. I hope this helps :+1:

1 Like