local c={}local A=false function c.new()local A=script.Parent:WaitForChild("\084\097\098\108\101")function c.AddRow(c)if A:FindFirstChild(c.Name)then return end local p=Instance.new("\070\114\097\109\101")p.Name=c.Name p.Size=UDim2.new(1,0,0,28)p.BackgroundColor3=Color3.new(1.0,.70196078431373,.27843137254902)p.BorderSizePixel=0 p.Parent=A local l=Instance.new("\084\101\120\116\076\097\098\101\108")l.Name="\085\115\101\114\110\097\109\101"l.Size=UDim2.new(.6,0,.8,0)l.Position=UDim2.new(.05,0,.1,0)l.BackgroundTransparency=1 l.TextColor3=Color3.new(1,1,1)l.TextScaled=true l.TextXAlignment=Enum.TextXAlignment.Left l.TextYAlignment=Enum.TextYAlignment.Center l.Text=c.Name l.Parent=p local u=Instance.new("\084\101\120\116\076\097\098\101\108")u.Name="\080\111\105\110\116\115"u.Size=UDim2.new(.25,0,.8,0)u.Position=UDim2.new(.7,0,.1,0)u.BackgroundTransparency=1 u.TextColor3=Color3.new(1,1,1)u.TextScaled=true u.TextXAlignment=Enum.TextXAlignment.Right u.TextYAlignment=Enum.TextYAlignment.Center u.Text=0 u.Parent=p local I=c:WaitForChild("\067\117\115\116\111\109\076\101\097\100\101\114\115\116\097\116\115")local Z=I:WaitForChild("\080\111\105\110\116\115")Z.Changed:Connect(function()u.Text=Z.Value u.LayoutOrder=Z.Value end)end function c.DeleteRow(c)local p=A:FindFirstChild(c.Name)if p then p:Destroy()return true else return false end end function c.Populate()local A=Players:GetChildren()for A,p in pairs(A)do c.AddRow(p)end end function c.Resize(c)local p=#A:GetChildren()-2 A.CanvasSize=UDim2.new(0,0,p,0)end Players.PlayerAdded:Connect(c.AddRow)Players.PlayerRemoving:Connect(c.DeleteRow)A.ChildAdded:Connect(c.Resize)A.ChildRemoved:Connect(c.Resize)return c end
-- Leaderboard table
local leaderboard = {}
local _table = false

function leaderboard.new()
	local self = script.Parent:WaitForChild("Table")

	function leaderboard.AddRow(player)
		-- Only add row for player if it does not exist
		if self:FindFirstChild(player.Name) then
			return
		end

		local row = Instance.new("Frame")
		row.Name = player.Name
		row.Size = UDim2.new(1, 0, 0, 28)
		row.BackgroundColor3 = Color3.new(255 / 255, 179 / 255, 71 / 255)
		row.BorderSizePixel = 0
		row.Parent = self

		local column1 = Instance.new("TextLabel")
		column1.Name = "Username"
		column1.Size = UDim2.new(0.6, 0, 0.8, 0)
		column1.Position = UDim2.new(0.05, 0, 0.1, 0)
		column1.BackgroundTransparency = 1
		column1.TextColor3 = Color3.new(1, 1, 1)
		column1.TextScaled = true
		column1.TextXAlignment = Enum.TextXAlignment.Left
		column1.TextYAlignment = Enum.TextYAlignment.Center
		column1.Text = player.Name
		column1.Parent = row

		local column2 = Instance.new("TextLabel")
		column2.Name = "Points"
		column2.Size = UDim2.new(0.25, 0, 0.8, 0)
		column2.Position = UDim2.new(0.7, 0, 0.1, 0)
		column2.BackgroundTransparency = 1
		column2.TextColor3 = Color3.new(1, 1, 1)
		column2.TextScaled = true
		column2.TextXAlignment = Enum.TextXAlignment.Right
		column2.TextYAlignment = Enum.TextYAlignment.Center
		column2.Text = 0
		column2.Parent = row

		-- Listen for point value changes and update leaderboard
		local leaderstats = player:WaitForChild("CustomLeaderstats")
		local points = leaderstats:WaitForChild("Points")
		points.Changed:Connect(function()
			column2.Text = points.Value
			column2.LayoutOrder = points.Value
		end)
	end

	function leaderboard.DeleteRow(player)
		local rowToDelete = self:FindFirstChild(player.Name)
		if rowToDelete then
			rowToDelete:Destroy()
			return true
		else
			return false
		end
	end

	function leaderboard.Populate()
		local playerList = Players:GetChildren()
		for i, player in pairs(playerList) do
			leaderboard.AddRow(player)
		end
	end

	function leaderboard.Resize(child)
		-- Size of table should be # of children - 2 to account to UIListLayout and this script
		local size = #self:GetChildren() - 2
		self.CanvasSize = UDim2.new(0, 0, size, 0)
	end

	-- Signals
	Players.PlayerAdded:Connect(leaderboard.AddRow)
	Players.PlayerRemoving:Connect(leaderboard.DeleteRow)

	self.ChildAdded:Connect(leaderboard.Resize)
	self.ChildRemoved:Connect(leaderboard.Resize)

	return leaderboard
end
