Custom Playerlist Update

I’ve made a Custom Playerlist with Teams with this video

And my problem is, every time someone changed their team, the other player need to reset in order to update it.

Code

local Teams = game:GetService(“Teams”)
local template = script.Example
local Player = game:GetService(“Players”)
local playere = game.Players.LocalPlayer
local playerlist = script.Parent.ScrollingFrame

game:GetService(‘StarterGui’):SetCoreGuiEnabled(Enum.CoreGuiType.PlayerList, false)

function clearlist()
for _,item in pairs(playerlist:GetChildren())do
if item:IsA(“TextLabel”) then
item:Destroy()
end
end
end

for _,team in ipairs(Teams:GetTeams())do
local frame = template:Clone()
frame.Name=team.Name

frame.LayoutOrder = #playerlist:GetChildren()

end

function FillList()
for _,player in pairs (Player:GetChildren()) do
if not playerlist:FindFirstChild(player.Name)then
local frame = template:Clone()
frame.Name = player.Name
frame.player.Text = player.name
frame.Parent = playerlist

		frame.LayoutOrder = playerlist[player.Team.Name].LayoutOrder + 1

		for _,object in pairs(player:GetChildren())do
			if object:IsA("Frame") and object.LayoutOrder >= frame.LayoutOrder and object ~= frame then 
				object.LayoutOrder = object.LayoutOrder + 1

				player:GetPropertyChangedSignal("TeamColor"):connect(function()
					frame.LayoutOrder = playerlist[player.Team.Name].LayoutOrder + 1
				end)
			end
		end
	end
end

end

FillList()

game.Players.PlayerAdded:Connect(FillList)
game.Players.PlayerRemoving:Connect(function(player)
if playerlist:FindFirstChild(player.Name)then
playerlist:FindFirstChild(player.name):Destroy()
end
end)

What it looks like

image

i have not tested this code but maybe it will point you in the correct direction

local teamsService = game:GetService(“Teams”)
local connections = {}

local function PlayerAdded(team, player)
    print(player, "Added", team.Name)
    -- this is where you would update the gui
end

local function PlayerRemoved(team, player)
   print(player, "Removed", team.Name)
    -- this is where you would update the gui
end

local function AddTeam(team)
    -- call PlayerAdded for all players in this team this will most likely be a empty list but just to be safe
    for i, player in ipairs(team:GetPlayers()) do
        PlayerAdded(team, player)
    end
    -- when a player is added to the team call the PlayerAdded function
    local addedConnection = team.PlayerAdded:Connect(function(player)
        PlayerAdded(team, player)
    end)
     -- when a player is removed from the team call the PlayerRemoved function
    local removedConnection = team.PlayerRemoved:Connect(function(player)
        PlayerRemoved(team, player)
    end)
    -- save the 2 connections into the connections table so that we can disconnect them later (if you are destroying the teams then they will be disconnected automatically but because i'm not sure what your doing i'm manually doing it just to make sure)
    connections[team] = {addedConnection, removedConnection}
end

local function RemoveTeam(team)
    -- call PlayerRemoved function for all players in the team you my not need to do this i'm not sure if Roblox automatically calls the remove event for all players when you destroy the team
    for i, player in ipairs(team:GetPlayers()) do
        PlayerRemoved(team, player)
    end
    -- disconnect PlayerAdded and PlayerRemoved events
    connections[team][1]:Disconnect()
    connections[team][2]:Disconnect()
    connections[team] = nil
end

-- loop all teams and call the AddTeam function
for i, team in ipairs(teamsService:GetTeams()) do
    AddTeam(team)
end
-- if a new team is added then call the AddTeam function
teamsService:ChildAdded(AddTeam)
-- if a team is removed then call the RemoveTeam function
teamsService:ChildRemoved(RemoveTeam)

Use a for loop
Example: for i, plrs in pairs do

You could use something like 5uphi’s Example

I fixed it by scripting my own custom playerlist

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.