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

[UNSOLVED] Why Does My DataStore Not Work With My Global LeaderBoard?

Asked by 1 year ago
Edited 1 year ago

I Have A Global Leaderboard And Leaderstats They Work Just Fine But When I Added A DataStore To Save The Data It Shows The Leaderstat as 0 And Doesn't Show Anything Here Are My Scripts:

This Is The Leaderstats And DataStore Script

local dataStoreService = game:GetService('DataStoreService')
local myDataStore = dataStoreService:GetOrderedDataStore('WinsLeaderBoard')

game.Players.PlayerAdded:Connect(function(player)
    local leaderstats = Instance.new('Folder')
    leaderstats.Name = 'leaderstats'
    leaderstats.Parent = player

    local Wins = Instance.new('IntValue')
    Wins.Name = 'Wins'
    Wins.Parent = leaderstats
    Wins.Value = 11

    local Coins = Instance.new('IntValue')
    Coins.Name = 'Coins'
    Coins.Parent = leaderstats
    Coins.Value = 0

    local data
    local success, errormessage = pcall(function()
        data = myDataStore:GetAsync(player.UserId..'-Wins')
    end)

    if data ~= nil then
        Wins.Value = data
    else
        print('There Was An Error Saving Data!')
        warn(errormessage)
    end
end)

game.Players.PlayerRemoving:Connect(function(player)
    local success, errormessage = pcall(function()
        myDataStore:SetAsync(player.UserId..'-Wins',player.leaderstats.Wins.Value)
    end)
    if success then
        print('Saved Data Successfuly Saved!')
    else
        print('There Was An Error While Saving Data!')
        warn(errormessage)
    end
end)

This Is The Script For The LeaderBoard

local DataStoreService = game:GetService('DataStoreService')
local WinsDataStore = DataStoreService:GetOrderedDataStore('WinsLeaderBoard')

local LeaderBoardPart = script.Parent.Parent.Parent
local RefreshRate = 10

local function RefreshLeaderboard()

    for i,Player in pairs(game.Players:GetPlayers()) do
        WinsDataStore:SetAsync(Player.UserId, Player.leaderstats.Wins.Value)
    end

    local Success, Error = pcall(function()

        local Data = WinsDataStore:GetSortedAsync(false, 10)
        local WinsPage = Data:GetCurrentPage()

        for Rank, SavedData in ipairs(WinsPage) do

            local Username = game.Players:GetNameFromUserIdAsync(tonumber(SavedData.key))
            local Wins = SavedData.value

            if Wins then
                local NewSample = script.Parent.GLTemplate:Clone()
                NewSample.Visible = true
                NewSample.Parent = LeaderBoardPart.SurfaceGui.Holder
                NewSample.Name = Username
                NewSample.Rank.Text = '#'..Rank
                NewSample.PlayerName.Text = Username
                NewSample.Value.Text = Wins
            end
        end
    end)
end

while true do

    for i,Frame in pairs(LeaderBoardPart.SurfaceGui.Holder:GetChildren()) do
        if Frame.Name ~= 'GLTemplate' and Frame:IsA('Frame') then
            Frame:Destroy()
        end
    end

    RefreshLeaderboard()
    task.wait(RefreshRate)

end

Thanks.

2 answers

Log in to vote
2
Answered by 1 year ago

Try seperating the datastore and the leaderstats and the leaderboard. Here is a fresh datastore if it helps:

local dataStoreService = game:GetService("DataStoreService")
local leaderstatsDataStore = dataStoreService:GetGlobalDataStore("leaderstats")

local loaded = {}

game.Players.PlayerAdded:Connect(function(player)
    local leaderstats = player:WaitForChild("leaderstats")
    if player.UserId > 0 and player.Parent then
        local leaderstatsdata = leaderstatsDataStore:GetAsync(player.UserId)    
        if leaderstatsdata ~= "Request Rejected" then
            if leaderstatsdata then         
                for i, stat in ipairs(leaderstats:GetChildren()) do 
                    local value = leaderstatsdata[stat.Name]
                    if value then
                        stat.Value = value
                    end
                end
            end
            loaded[player] = true
        end
    end
end)

game.Players.PlayerRemoving:Connect(function(player)
    local leaderstats = player:FindFirstChild("leaderstats")
    if leaderstats then
        if loaded[player] then
            local leaderstatsData = {}
            for i, stat in ipairs(leaderstats:GetChildren()) do
                leaderstatsData[stat.Name] = stat.Value
            end
            leaderstatsDataStore:SetAsync(player.UserId, leaderstatsData)
        end
    end
    loaded[player] = nil

end)

Hope this helps!!!!

Ad
Log in to vote
0
Answered by 1 year ago

I tried This But It Still Does Not Show Anything On The LeaderStats Or Leaderboard, Thanks.

Answer this question