Help with Teams

I’m trying to make a list of players on a team by cloning a button template for every player thats on the team. The problem is, whenever a player gets added to the team, it doesn’t update the UI. How can I make it so that it’s constantly checking to see who’s on the team (if anyone at all) and then clone the template based off of how many players are on the team?

	for _, playerOnTeam in pairs(game.Players.LocalPlayer.Team:GetPlayers()) do
		local template = partyMemberList.template_playerinfo:Clone()
		template.Visible = true
		template.Parent = partyMemberList
		template.username.Text = playerOnTeam.Name
		template.pfp.Image = game:GetService('Players'):GetUserThumbnailAsync(playerOnTeam.UserId, Enum.ThumbnailType.HeadShot, Enum.ThumbnailSize.Size420x420)
		template.player_level.Text = 'lvl. ' .. playerOnTeam:WaitForChild('leaderstats').Level.Value
		if playerOnTeam.Name == playerOnTeam.Team.Name then
			template.crown.Visible = true
			template.LayoutOrder = 0
			template.Parent.party_options["button_leave/disband"].Text = 'DISBAND'
		else
			template.crown.Visible = false
			template.LayoutOrder = 1
			template.Parent.party_options["button_leave/disband"].Text = 'LEAVE'
		end
	end
1 Like

There is an event for every team called PlayerAdded. Use this to update the UI. Also you may want to use a remote event for handling the UI on the client as you will check on the server when the player is added
Server:

local Team = -- Put the path for your team here
Team.PlayerAdded:Connect(function(plr)
   game.ReplicatedStorage.CloneUI.FireClient(plr)
end)

Client:

local player = game.Players.LocalPlayer
game.ReplicatedStorage.CloneUI.OnClientEvent:Connect(function()
		local template = partyMemberList.template_playerinfo:Clone()
		template.Visible = true
		template.Parent = partyMemberList
		template.username.Text = player.Name
		template.pfp.Image = game:GetService('Players'):GetUserThumbnailAsync(player.UserId, Enum.ThumbnailType.HeadShot, Enum.ThumbnailSize.Size420x420)
		template.player_level.Text = 'lvl. ' .. player:WaitForChild('leaderstats').Level.Value
		if player. Name == player.Team.Name then
			template.crown.Visible = true
			template.LayoutOrder = 0
			template.Parent.party_options["button_leave/disband"].Text = 'DISBAND'
		else
			template.crown.Visible = false
			template.LayoutOrder = 1
			template.Parent.party_options["button_leave/disband"].Text = 'LEAVE'
		end
	end)
2 Likes

You’ll want to use event listeners in Roblox, specifically the PlayerAdded and PlayerRemoving events. This way, you can continuously check the number of players on the team and update the UI accordingly.

Here’s an approach you can follow:

Create functions to update the UI when players are added or removed from the team.

Connect these functions to the events PlayerAdded and PlayerRemoving on the player’s team.

Here is a sample implementation:

local Players = game:GetService("Players")

local LocalPlayer = Players.LocalPlayer

local partyMemberList = script.Parent:WaitForChild('partyMemberList') -- Assuming this is the parent container for your UI

local playerTemplate = partyMemberList.template_playerinfo

local function updatePlayerList()

    -- Clear the existing UI items to refresh

    for _, child in ipairs(partyMemberList:GetChildren()) do

        if child:IsA("Frame") and child.Name ~= 'template_playerinfo' then

            child:Destroy()

        end

    end

    -- Iterate through the players on the team and update the UI

    for _, playerOnTeam in pairs(LocalPlayer.Team:GetPlayers()) do

        local template = playerTemplate:Clone()

        template.Visible = true

        template.Parent = partyMemberList

        template.username.Text = playerOnTeam.Name

        template.pfp.Image = Players:GetUserThumbnailAsync(playerOnTeam.UserId, Enum.ThumbnailType.HeadShot, Enum.ThumbnailSize.Size420x420)

        template.player_level.Text = 'lvl. ' .. playerOnTeam:WaitForChild('leaderstats').Level.Value

        if playerOnTeam.Name == LocalPlayer.Team.Name then

            template.crown.Visible = true

            template.LayoutOrder = 0

            template.Parent.party_options["button_leave/disband"].Text = 'DISBAND'

        else

            template.crown.Visible = false

            template.LayoutOrder = 1

            template.Parent.party_options["button_leave/disband"].Text = 'LEAVE'

        end

    end

end

-- Connect to team player addition and removal

LocalPlayer.Team.PlayerAdded:Connect(updatePlayerList)

LocalPlayer.Team.PlayerRemoving:Connect(updatePlayerList)

-- Initialize the player list on startup

updatePlayerList()
2 Likes

Thank you everyone for helping

1 Like

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