Help on local server leaderboard

Hello, so I am scripting a leaderboard that show’s who has the most money in the server. I got the username and amount of cash showing I need it to be in order from most cash - least cash and I also want it to show what place you are in

function leaderboard:RefreshLocalLeaderboard()
	local Model = self.Model
	local List = Model:FindFirstChild("Items", true)
	local playerValues = {}

	for _, v in pairs(List:GetChildren()) do
		if v:IsA("TextLabel") then
			v:Destroy()
		end
	end

	for _, player in pairs(game.Players:GetPlayers()) do
		playerValues[player.UserId] = player:WaitForChild("leaderstats"):WaitForChild("Cash").Value
	end

	table.sort(playerValues)

	for i, v in pairs(playerValues) do
        local New = Instance.new("TextLabel")
        New.TextColor3 = Color3.new(1, 1, 1)
        New.BackgroundTransparency = 1
        New.Size = UDim2.new(1, 0, 0.1, 0)
        New.Font = Enum.Font.SourceSansBold
        New.TextScaled = true
        New.Parent = List
        New.Name = game.Players:GetNameFromUserIdAsync(i)
        New.Text =  "#" .. I need help on the # part .." " .. game.Players:GetNameFromUserIdAsync(i) .. " $" .. v
        New.Visible = true
	end
end
2 Likes

Do not index using player.userId, use their cash value as the index. Then, you can use ipairs for the playerValues loop.

2 Likes

Yes I got the # part done but now it doesn’t list the player’s name

I forgot to mention your list must be an array.

But I need it to be a dictionary so I can get the player’s name as well.

Nevermind I found out how to do it its by adding a new table and inserting the name at the same time you are getting the data