I'm trying to get a playerlist of the server and put it on a GUI. It works sometimes when you are already inside the server when the player joins. But, when you join a server with players already in it, the playerlist displays nothing. Also, when the player leaves the server the player name is still on the GUI for some reason.. I tried to solve this problem but I do not know what is wrong with the code. If anyone can help, that would be much appreciated.

local players = game:GetService("Players")
local players_table = {}
--gui stuff
local plrbutton = game:GetService("ReplicatedStorage"):WaitForChild("PlayerButton")
local scrollframe = script.Parent.ScrollingFrame

players.PlayerRemoving:Connect(function(leaving)
	-- check if a player is leaving
	if leaving then
		-- if so, delete dictionary entry and delete button
		players_table[leaving] = nil
		scrollframe:FindFirstChild(leaving.Name):Destroy()
	end
end)

players.PlayerAdded:Connect(function()
	-- loop through players
	local chil = players:GetChildren()
	for c = 1, #chil do
		-- check if present in dictionary
		if players_table[chil[c].Name] == nil then		
			-- add to dictionary
			players_table[chil[c].Name] = 1
			print(players_table[1])
		end
	end
end)

local function button()
	for i,v in pairs(players_table) do
		--clone button into gui with playername on it
		local clone = button:Clone()
		clone.Parent = scrollframe
		clone.Name = v.Name
		clone.Text = v.Name
	end
end

2 comments

1

PlayerAdded is server side only. When I say that, I mean it has no use on the client. Just use LocalPlayer. The LocalPlayer will have added before the LocalScript runs, so you're waiting for a player that isn't you to join.

0

And if you're going to steal one of Shirooo's scripts, at least do a good job at hiding it.