Positioning help

Hey, I’m trying to make it so that the player in the leaderboard goes under their team, and if another player joins, they go under that player, etc, just how the roblox leaderboard works.
Here is my current code. The teams work are are auto created nicely, but I have no idea how to properly position the player.

Here is my code.

local Players = game:GetService("Players");
local Teams = game:GetService("Teams");
local TextService = game:GetService("TextService");
local MainScroll = script.Parent.MainScroll
local TeamTemplate = script.Parent.MainScroll.Templates.Character
local StarterGui = game:GetService("StarterGui")
StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.PlayerList, false)

function PopulateTeams()
	for i,v in pairs(MainScroll:GetChildren()) do
		if v:IsA("TextLabel") and v.Name == "Character" then
			v:Destroy()
		end
	end
	for _, team in ipairs(Teams:GetTeams()) do
		local plrs = team:GetPlayers()
		if #plrs >= 1 then
			local cloned = TeamTemplate:Clone()
			cloned.Text = team.Name
			cloned.Back.Text = team.Name
			cloned.Parent = MainScroll
			cloned.Color.ImageColor3 = team.TeamColor.Color
			cloned.LayoutOrder = #MainScroll:GetChildren()
			cloned.Visible = true
		end
	end
end

function PopulatePlayers()
	for i,v in pairs(MainScroll:GetChildren()) do
		if v:IsA("TextLabel") and v.Name == "Player" then
			v:Destroy()
		end
	end
	for i,v in pairs(Players:GetChildren()) do
		local PlayerTemplate = script.Parent.MainScroll.Templates.Player
		local playercloned = PlayerTemplate:Clone()
		local playerteam = v.Team
		playercloned.PlayerName.Text = v.Name
		playercloned.Parent = MainScroll
		-- playercloned.LayoutOrder = ?
		playercloned.Visible = true		
	end
end

for i,v in pairs(Teams:GetTeams()) do
	v.PlayerAdded:Connect(PopulateTeams)
	v.PlayerAdded:Connect(PopulatePlayers)
	v.PlayerRemoved:Connect(PopulateTeams)
	v.PlayerRemoved:Connect(PopulatePlayers)
end

Hello, the easiest way to do this would be to use a UI List Layout. By putting this into the scrolling frame, the list will automatically be positioned.

However if you wanted to code it yourself, you would have to have some type of counter, which figures out how many people are in the list, then times that value by the position.

Both methods would work.

1 Like