Leaderboard not working

I’m having trouble creating a leaderboard system that displays players dancing on a podium. Something in the code isn’t working and I cant figure out what it is. (It also isn’t outputting any errors)

It is also worth noting that there are two of these leaderboards in-game, which is why there are two values for the leaderstats.

leaderboards:


image

Code:

local players = game:GetService("Players")
local dataStoreService = game:GetService("DataStoreService")

local cashDataStore = dataStoreService:GetDataStore("cashDatabase")
local winsDataStore = dataStoreService:GetDataStore("winsDatabase")

local cashLeaderboard = dataStoreService:GetOrderedDataStore("cashLeaderboard")
local winsLeaderboard = dataStoreService:GetOrderedDataStore("winsLeaderboard")

players.PlayerRemoving:Connect(function(player)
	local cashValue = player.leaderstats.Cash.Value
	local winsValue = player.leaderstats.Wins.Value

	cashLeaderboard:SetAsync(player.UserId, cashValue)
	winsLeaderboard:SetAsync(player.UserId, winsValue)
end)

function format(value)
	local formatted = tostring(value)
	while true do
		formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", "%1,%2")
		if k == 0 then
			break
		end
	end
	return formatted
end

coroutine.wrap(function()
	while true do
		local cashResults = cashLeaderboard:GetSortedAsync(false, 3, 1):GetCurrentPage()
		for i, v in ipairs(cashResults) do
			local userId = v.key
			local rank = tostring(i)
			local value = v.value
			local playerName = players:GetNameFromUserIdAsync(userId)
			local place = workspace.Lobby.DancingLeaderboard.TopCashPlayers:FindFirstChild(rank)
			if place then
				place.Statue.Humanoid:ApplyDescription(players:GetHumanoidDescriptionFromUserId(userId))
				local anim
				pcall(function()
					anim = place.Statue.Humanoid.Animator:LoadAnimation(place.Statue.Animation)
				end)
				anim:Play()
				local formatted = format(value)
				place.Stats.BillboardGui.Frame.PlayerName.Text = "#" .. rank .. ": " .. playerName
				place.Stats.BillboardGui.Frame.MoneyAmount.Text = "Cash: " .. formatted
			end
		end

		local winsResults = winsLeaderboard:GetSortedAsync(false, 3, 1):GetCurrentPage()
		for i, v in ipairs(winsResults) do
			local userId = v.key
			local rank = tostring(i)
			local value = v.value
			local playerName = players:GetNameFromUserIdAsync(userId)
			local place = workspace.Lobby.DancingLeaderboard2.TopWinsPlayers:FindFirstChild(rank)
			if place then
				place.Statue.Humanoid:ApplyDescription(players:GetHumanoidDescriptionFromUserId(userId))
				local anim
				pcall(function()
					anim = place.Statue.Humanoid.Animator:LoadAnimation(place.Statue.Animation)
				end)
				anim:Play()
				local formatted = format(value)
				place.Stats.BillboardGui.Frame.PlayerName.Text = "#" .. rank .. ": " .. playerName
				place.Stats.BillboardGui.Frame.WinsAmount.Text = "Wins: " .. formatted
			end
		end

		wait(60)
	end
end)

add a print under both of the if place then and let me know if it prints anything, please

1 Like

also, what exactly is going wrong? is the player not being loaded, is the money counter not showing the right amount, i assume all of the above but if there’s a more specific problem going on please let me know

Without testing this (and I have too many now) this is just a guess …
Tried to clean it up a bit … didn’t add error checking however.

local players = game:GetService("Players")
local dataStoreService = game:GetService("DataStoreService")

local cashLeaderboard = dataStoreService:GetOrderedDataStore("cashLeaderboard")
local winsLeaderboard = dataStoreService:GetOrderedDataStore("winsLeaderboard")

function format(value)
    return string.format("%d", value)
end

function updateLeaderboard(leaderboard, topPlayersFolder, getValueFunction)
    local results = leaderboard:GetSortedAsync(false, 3, 1):GetCurrentPage()
    for i, v in ipairs(results) do
        local userId = v.key
        local rank = tostring(i)
        local value = v.value
        local playerName = players:GetNameFromUserIdAsync(userId)
        local place = topPlayersFolder:FindFirstChild(rank)
        if place then
            place.Statue.Humanoid:ApplyDescription(players:GetHumanoidDescriptionFromUserId(userId))
            local anim = place.Statue.Humanoid.Animator:LoadAnimation(place.Statue.Animation)
            anim:Play()
            local formatted = format(value)
            place.Stats.BillboardGui.Frame.PlayerName.Text = "#" .. rank .. ": " .. playerName
            place.Stats.BillboardGui.Frame.ValueAmount.Text = getValueFunction(formatted)
        end
    end
end

players.PlayerRemoving:Connect(function(player)
    local cashValue = player.leaderstats.Cash.Value
    local winsValue = player.leaderstats.Wins.Value
    cashLeaderboard:SetAsync(player.UserId, cashValue)
    winsLeaderboard:SetAsync(player.UserId, winsValue)
end)

while true do
    updateLeaderboard(cashLeaderboard, workspace.Lobby.DancingLeaderboard.TopCashPlayers, function(formatted) return "Cash: " .. formatted end)
    updateLeaderboard(winsLeaderboard, workspace.Lobby.DancingLeaderboard2.TopWinsPlayers, function(formatted) return "Wins: " .. formatted end)
    task.wait(60)
end

2 Likes

Nothing got printed. What is going on is neither the player nor the money counter is being loaded.

have you tested it in-game? i’ll try and find a problem in the code, but the problem may be that you’re in studio

With a little bit of tweaking, this worked! Thank you to everybody for responding.

2 Likes

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