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

Refresh button to get every player in server doesn't work?

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)

1 answer

✓ Accepted answer

You could do it with one for loop:

for _, playername in pairs(game:GetService("Players"):GetPlayers()) do
	playerlist:ClearAllChildren()
	local clone = playerbutton:Clone()
	clone.Name = playername.Name
	clone.Text = playername.Name
	clone.Parent = playerlist
end

Upon refresh, it'll clear everything, and re-add the players back in.

2 comments

0

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?

0

check if they're of the textbutton class

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