I'm trying to make a refresh button to get all the player names inside the server on a text button. I do not want a duplicate player name inside the gui when I click the refresh button, so i'm making a loop to check through all the textlabel names to make sure the player is not already in the gui. But when I test it it would print "CLICK" 2 times when I click the refresh button only once?? And it does not prevent duplicate textlabels of the same player name. I need the GUI so when I click the refresh button it would get all the players in the server and input them inside of the gui. If anyone could help it would be appreciated.
Here is a GIF of me trying to refresh the playerlist.. https://imgur.com/lvc5O3d
local refresh = script.Parent.RefreshButton
local playerlist = script.Parent.Playerlist
local playerbutton = game:GetService("ReplicatedStorage"):WaitForChild("PlayerButton")
refresh.MouseButton1Click:Connect(function()
--Get Players
for i,playername in pairs(game:GetService("Players"):GetPlayers()) do
--Check if player is already in playerlist. If not then make a label for player
for i,children in pairs(playerlist:GetChildren()) do
if playername.Name ~= children.Name then
print("CLICK")
local clone = playerbutton:Clone()
clone.Name = playername.Name
clone.Text = playername.Name
clone.Parent = playerlist
end
end
end
end)

2 comments
Thanks it works, but ClearAllChildren clears out my UIListLayout which I need in order to organize the buttons. Is there any way to only remove the textbuttons?
check if they're of the textbutton class