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

OrderedDatastore doesnt save value?

Asked by 2 years ago

I'm trying to make a leaderboard where it shows what person had the most coins, but every time a player leaves, it doesn’t save their coins which means if they were to have the most coins in the game, they wouldn’t appear on the leaderboard

Server Script:

--[[

    coin lederbord wow - not lam

]]
-- services
local datastoreservice = game:GetService('DataStoreService')

-- components
local datastore = datastoreservice:GetOrderedDataStore('datastorelederbord')
local pages = datastore:GetSortedAsync(false, 3,5)
local key = 'leaderboardData'
local templateFolder = script.Parent.Parent.Templates

while true do
    local data = pages:GetCurrentPage()
    warn(data)


    function abbreviateNumber (n)
        local s = tostring(math.floor(n))
        return string.sub(s, 1, ((#s - 1) % 3) + 1) .. ({"", "K", "M", "B", "T", "QA", "QI", "SX", "SP", "OC", "NO", "DC", "UD", "DD", "TD", "QAD", "QID", "SXD", "SPD", "OCD", "NOD", "VG", "UVG"})[math.floor((#s - 1) / 3) + 1]
    end


    for current, v in ipairs(data) do
        if current <= 3 then
            local ui = templateFolder:FindFirstChild(current)
            ui.Parent = script.Parent.ScrollingFrame
            ui.Name = game:GetService('Players'):GetNameFromUserIdAsync(v.key)
            ui.place.Text = "#"..current
            ui.playerName.Text = game:GetService('Players'):GetNameFromUserIdAsync(v.key)
            ui.count.Text = abbreviateNumber(v.value)
            current += 1
            warn('ui made')
        else
            local a = tostring(current)
            local b = string.split(v.key,'xp9Datastore!Key')
            local ui = templateFolder:FindFirstChild('normal')
            ui.Parent = script.Parent.ScrollingFrame
            ui.Name = b
            ui.place.Text = "#"..current
            ui.playerName.Text = b
            ui.count.Text = abbreviateNumber(v.value)
            current += 1
            warn('ui made')
        end

        if pages.IsFinished then
            warn('done')
            break
        else
            warn('next page')
            pages:AdvanceToNextPageAsync()
        end
    end
    task.wait(60)
end

game:GetService('Players').PlayerRemoving:Connect(function(plr)
    local h = tonumber(plr:FindFirstChild('coins').Value)
    datastore:SetAsync(plr.UserId..key,h)
    warn('saved')
end)

game:BindToClose(function()
    for _, v in pairs(game:GetService('Players'):GetChildren()) do
        local h = tonumber(v:FindFirstChild('coins').Value)
        datastore:SetAsync(v.UserId,h)
        warn('saved')
    end
end)

Answer this question