Leaderboard Will Not Work

I was trying to create a leaderboard that would show the top 10 players, but it doesn’t work. This is my first time doing something like this and I’m not really sure what the issue is. Nothing throws an error it just doesn’t seem to run any of the for loops. I already spent maybe an hour or so trying figure out the problem and I’m out of ideas. Any explanations would be greatly appreciated.

    local frames = script.Parent

    --Datastore--
    local datastoreService = game:GetService("DataStoreService")
    local levelsClearedStore = datastoreService:GetOrderedDataStore("LevelsCleared")

    while true do
    	local pages = levelsClearedStore:GetSortedAsync(false, 10, 1, 10) -- Is asending, amount per page, min, max
    	local topPlayers = pages:GetCurrentPage()
    	
    	local inputs = {}
    	
    	for i,v in ipairs(topPlayers) do
    		local userID = i.key
    		local levelsCleared = i.value
    		local username = "error"
    		username = game.Players:GetNameFromUserIdAsync(userID)
    		
    		local image = game.Players:GetUserThumbnailAsync(userID, Enum.ThumbnailType.HeadShot, Enum.ThumbnailSize.Size150x150)
    		
    		table.insert(inputs,{username,levelsCleared,image})
    	end
    	
    	--Changes Frames--
    	local currentFrame = 0
    	for i, v in pairs(inputs) do
    		currentFrame += 1
    		
    		local selectedFrame = frames[currentFrame]
    		selectedFrame.PlayerName.Text = v[1]
    		selectedFrame.Value.Text = v[2]
    		selectedFrame.PlayerImage.Image = v[3]
    	end
    	
    	wait(5)
    end 

frames is a frame that holds a set of 10 frames which each have their own labels that need to be changed.

1 Like

Maybe update the values at the beginning because the values are not saved while it gets the player’s datastore and add replace wait(5) to maybe 2 minutes.

Okay, I was unsure if I needed to do it or how it works exactly.

1 Like

It still doesn’t work… Updating the data first made absolutely no change except it updates the data. The wait time doesn’t really matter as of right now, it really won’t run the for loops for whatever reason.

Can I see the code you’re trying to fix?

local frames = script.Parent

local serverStorage = game.ServerStorage
local playerDataFolder = serverStorage.PlayerData

local datastoreService = game:GetService("DataStoreService")
local levelsClearedStore = datastoreService:GetOrderedDataStore("LevelsCleared")

while true do
	--Updates Datastore  (This is the only new thing...)--
	for i, v in pairs (game.Players:GetChildren()) do
		if v.UserId > 0 then
			levelsClearedStore:UpdateAsync(v.UserId, function(oldVal)
				local currentPlayerDataFolder = playerDataFolder[v.Name]
				if currentPlayerDataFolder then
					return currentPlayerDataFolder.LevelsCleared.Value
				end
			end)
		end
	end
	
	local pages = levelsClearedStore:GetSortedAsync(false, 10, 1, 10)
	local topPlayers = pages:GetCurrentPage()
	
	local inputs = {}
	
	for i,v in ipairs(topPlayers) do
		local userID = i.key
		local levelsCleared = i.value
		local username = "ERROR"
		username = game.Players:GetNameFromUserIdAsync(userID)
		
		local image = game.Players:GetUserThumbnailAsync(userID, Enum.ThumbnailType.HeadShot, Enum.ThumbnailSize.Size150x150)
		
		table.insert(inputs,{username,levelsCleared,image})
	end
	
        --Changes Frames--
	local currentFrame = 0
	for i, v in pairs(inputs) do
		currentFrame += 1
		
		local selectedFrame = frames[currentFrame]
		selectedFrame.PlayerName.Text = v[1]
		selectedFrame.Value.Text = v[2]
		selectedFrame.PlayerImage.Image = v[3]
	end
	
	wait(5)
end

Forgot to tell but I also need you screenshot the GUI and script in the explorer so I can try to replicate so I can fix it, otherwise It’ll take longer because of confusion.

Here it is:

1 Like
local frames = script.Parent

local serverStorage = game.ServerStorage
local playerDataFolder = serverStorage.PlayerData

local datastoreService = game:GetService("DataStoreService")
local levelsClearedStore = datastoreService:GetOrderedDataStore("LevelsCleared")

while true do
	--Updates Datastore  (This is the only new thing...)--
	for i, v in pairs (game.Players:GetChildren()) do
		if v.UserId > 0 then
			levelsClearedStore:UpdateAsync(v.UserId, function(oldVal)
				local currentPlayerDataFolder = playerDataFolder[v.Name]
				if currentPlayerDataFolder then
					return currentPlayerDataFolder.LevelsCleared.Value
				end
			end)
		end
	end

	local pages = levelsClearedStore:GetSortedAsync(false, 10, 0, 10)
	local topPlayers = pages:GetCurrentPage()

	local inputs = {}

	for i,v in ipairs(topPlayers) do
		local userID = v.key
		local levelsCleared = v.value
		local username = "ERROR"
		username = game.Players:GetNameFromUserIdAsync(userID)

		local image = game.Players:GetUserThumbnailAsync(userID, Enum.ThumbnailType.HeadShot, Enum.ThumbnailSize.Size150x150)

		table.insert(inputs,{username,levelsCleared,image})
	end

	--Changes Frames--
	local currentFrame = 0
	for i, v in pairs(inputs) do
		currentFrame += 1

		local selectedFrame = frames[currentFrame]
		selectedFrame.PlayerName.Text = v[1]
		selectedFrame.Value.Text = v[2]
		selectedFrame.PlayerImage.Image = v[3]
	end

	wait(60)
end

I managed to get the script fixed. There’s no way you don’t see the error but there is an error saying that you’re trying to index an index to a key and userid instead of the value ://

local userID = i.key
local levelsCleared = i.value

If you really don’t see the error then idk but that’s how it’s fixed. :PP

Anyway it has to wait(60) higher because if it waits lower than 60 seconds then the datastore save request will have a warning saying that you are saving the data above the minute, and probably you can lose data (unsure if you can lose data) :>

2 Likes

Wow I hated every second of this. There was a lot of factors that went into this which caused a lot to fail. Thanks so much for this answer but I’m pretty sure this wasn’t the main cause and it was actually just me being stupid again. - Yes this wasn’t the main problem.

1 Like

Lol, anyway no problem and If it wasn’t the main cause then probably updating the datastore fixes it. Also you don’t have to call yourself stupid, everyone makes mistakes anyway.

2 Likes

It was really me not understanding the parameters…

3 Likes