Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

how to make a deaths leaderboarrd?

Asked by 4 years ago

make a leaderboard that shows rank,playername and number of deaths but i keep getting an errormsg : ServerScriptService.DeathsLeaderboardHandler:7: attempt to index nil with 'GetCurrentPage' cript:

local DataStoreService = game:GetService("DataStoreService")
local DeathsLeaderboard = DataStoreService:GetOrderedDataStore("DeathsLeaderboard")

local function updateLeaderboard()
    local success, errorMessage = pcall(function()
        local Data = DeathsLeaderboard:GetAsync(false, 5)
        local DeathsPage = Data:GetCurrentPage()
        for Rank, data in ipairs(DeathsPage) do
            local userName = game.Players:GetNameFromUserIdAsync(tonumber(data.key))
            local Name = userName
            local Deaths = data.value
            local isOnLeaderboard = false
            for i, v in pairs(game.Workspace.Spawn.DeathsGlobalLeaderboard.SurfaceGui.Holder:GetChildren()) do
                if v.Player.Text == Name then
                    isOnLeaderboard = true
                    break
                end
            end

            if Deaths and isOnLeaderboard == false then
                local newLBFrame = game.ReplicatedStorage:WaitForChild("DeathsLeaderboardFrame"):Clone()
                newLBFrame.Player.Text = Name
                newLBFrame.Deaths.Text = Deaths
                newLBFrame.Rank.Text = "#"..Rank
                newLBFrame.Position = UDim2.new(0, 0, newLBFrame.Position.Y.Scale + (.08 * #game.Workspace.Spawn.DeathsGlobalLeaderboard.SurfaceGui.Holder:GetChildren()), 0)
                newLBFrame.Parent = game.Workspace.Spawn.DeathsGlobalLeaderboard.SurfaceGui.Holder
            end
        end
    end)

    if not success then
        warn(errorMessage)
    end
end

while true do

    for _, player in pairs(game.Players:GetPlayers()) do
        DeathsLeaderboard:SetAsync(player.UserId, player.leaderstats.Deaths.Value)
    end

    for _, frame in pairs(game.Workspace.Spawn.DeathsGlobalLeaderboard.SurfaceGui.Holder:GetChildren()) do
        frame:Destroy()
    end

    updateLeaderboard()
    print("Updated Deaths Leaderboard!")

    wait(120)
end

i am getting the Updated Deaths Leaderboard msg though.

1 answer

Log in to vote
0
Answered by 4 years ago

guys i figured it out but the msg when i was about to delete this question told me other wise

i put GetSortedAsync rather than GetAsync

line 6

local DataStoreService = game:GetService("DataStoreService")
local DeathsLeaderboard = DataStoreService:GetOrderedDataStore("DeathsLeaderboard")

local function updateLeaderboard()
    local success, errorMessage = pcall(function()
        local Data = DeathsLeaderboard:GetSortedAsync(false, 5)
        local DeathsPage = Data:GetCurrentPage()
        for Rank, data in ipairs(DeathsPage) do
            local userName = game.Players:GetNameFromUserIdAsync(tonumber(data.key))
            local Name = userName
            local Deaths = data.value
            local isOnLeaderboard = false
            for i, v in pairs(game.Workspace.Spawn.DeathsGlobalLeaderboard.SurfaceGui.Holder:GetChildren()) do
                if v.Player.Text == Name then
                    isOnLeaderboard = true
                    break
                end
            end

            if Deaths and isOnLeaderboard == false then
                local newLBFrame = game.ReplicatedStorage:WaitForChild("DeathsLeaderboardFrame"):Clone()
                newLBFrame.Player.Text = Name
                newLBFrame.Deaths.Text = Deaths
                newLBFrame.Rank.Text = "#"..Rank
                newLBFrame.Position = UDim2.new(0, 0, newLBFrame.Position.Y.Scale + (.08 * #game.Workspace.Spawn.DeathsGlobalLeaderboard.SurfaceGui.Holder:GetChildren()), 0)
                newLBFrame.Parent = game.Workspace.Spawn.DeathsGlobalLeaderboard.SurfaceGui.Holder
            end
        end
    end)

    if not success then
        warn(errorMessage)
    end
end

while true do

    for _, player in pairs(game.Players:GetPlayers()) do
        DeathsLeaderboard:SetAsync(player.UserId, player.leaderstats.Deaths.Value)
    end

    for _, frame in pairs(game.Workspace.Spawn.DeathsGlobalLeaderboard.SurfaceGui.Holder:GetChildren()) do
        frame:Destroy()
    end

    updateLeaderboard()
    print("Updated Deaths Leaderboard!")

    wait(120)
end
Ad

Answer this question