Leaderboard doesn't work

My friend and I are trying to make a leaderboard. For some reason, nothing in the for loop works. Does anyone have ideas?

local DataStoreService = game:GetService("DataStoreService")
local DS2 = game.ServerScriptService.DataStore2
local LevelDataStore = DataStoreService:GetOrderedDataStore("LevelDataStore")

local LeaderboardPart = script.Parent.Parent.Parent
local RefreshRate = 5

function RefreshLeaderboard()

    for i, plr in pairs(game.Players:GetPlayers()) do

        LevelDataStore:SetAsync(plr.UserId, plr.leaderstats:WaitForChild("Level").Value)
    end

    local success, errormsg = pcall(function()
        

        local Data = LevelDataStore:GetSortedAsync(false, 10)
        local LevelPage = Data:GetCurrentPage()

        for i, v in ipairs(LevelPage) do
            
            print(v)
        
            local UserName = game.Players:GetNameFromUserIdAsync(tonumber(v.key))
            
            print(game.Players:GetNameFromUserIdAsync(tonumber(v.key)))

            local Level = v.value

            if Level then
                
                print("Level Is True")

                local NewSample = script.Parent.Template:Clone()

                NewSample.Parent = LeaderboardPart.SurfaceGui.ScrollingFrame
                NewSample.Name = UserName

                NewSample.Rank.Text = "# " .. i
                NewSample.PlrName.Text = UserName
                NewSample.Level.Text = Level

            end
        end
    end)
end


while wait(1) do 
    
    print("While wait here")

    for i, v in pairs(LeaderboardPart.SurfaceGui.ScrollingFrame:GetChildren()) do
        if v.Name ~= "Template" and v:IsA("Frame") then
            v:Destroy()
        end
    end
    
    RefreshLeaderboard()
    wait(RefreshRate)
end
1 Like

Will this work?

while true do
    print("check")
    for i, v in pairs(LeaderboardPart.SurfaceGui.ScrollingFrame:GetChildren()) do
        if v.Name ~= "Template" and v:IsA("Frame") then
            v:Destroy()
        end
    end
    RefreshLeaderboard()
    wait(RefreshRate)
end

Does it show any errors??? If so share them! Also, try and print with trial and error to see which parts work within the code.

There are no errors.

–Character limit

Like I said, first try to print on some areas and see which areas are printed. From there we can move on. (The unprinted area is maybe where it needs fixing).

We did do that, and it didn’t work past “local UserName”.

Are you sure API Services are on?
You can turn them on by going to Game Settings - Security - API Services and turn them on.
Also, it’s better to test DataStore in the Roblox Client, not studio.

Yeah try what @captan40000 said or:

Maybe try and replace Players with :GetService("Players")

I’m not too sure on this one tho.

All of that has been done.

–Character limit

1 Like