Search code examples
luarobloxdatastore

Is there anyway to fix this Error with my Leaderboard?


I'm getting this Error when my Leaderboard tries to update. 502: API Services rejected request with error. Invalid value format for datastore type Sorted. Parameter name: value This is the script:

local CoinsDS = game:GetService("DataStoreService"):GetOrderedDataStore("MoneyLeaderboard")
local suffixes = {'','K','M','B','T','qd','Qn','sx','Sp','O','N','de','Ud','DD','tdD','qdD','QnD','sxD','SpD','OcD','NvD','Vgn','UVg','DVg','TVg','qtV','QnV','SeV','SPG','OVG','NVG','TGN','UTG','DTG','tsTG','qtTG','QnTG','ssTG','SpTG','OcTG','NoAG','UnAG','DuAG','TeAG','QdAG','QnAG','SxAG','SpAG','OcAG','NvAG','CT'}
local function format(val)
    for i=1, #suffixes do
        if tonumber(val) < 10^(i*3) then
            return math.floor(val/((10^((i-1)*3))/100))/(100)..suffixes[i]
        end
    end
end

while true do
    for _,Player in pairs(game.Players:GetPlayers()) do
        local Leaderstats = Player:FindFirstChild("leaderstats")
        if Leaderstats then
            local coins = Leaderstats:FindFirstChild("CloudCoins")
            if coins then
                CoinsDS:SetAsync(Player.UserId, coins.Value)
            end
        end
    end
    local boards = workspace:WaitForChild("LeaderboardStuff")
    local sellected = boards:WaitForChild("ViewCoins")
    local Count = boards:WaitForChild("CountCoins")
    local Info = Count:WaitForChild("Info")
    for i,v in pairs(sellected:WaitForChild("GUI"):GetChildren()) do
        v:Destroy()
    end
    for _,v in pairs(boards:WaitForChild("TopCoins"):GetChildren()) do
        v:Destroy()
    end
    local Success, Err = pcall(function()
        local Data = CoinsDS:GetSortedAsync(false, 10)
        local LPage = Data:GetCurrentPage()
        for i, v in pairs(LPage) do
            if v.key ~= nil and tonumber(v.key) and tonumber(v.key) > 0 then
                local name = game:GetService("Players"):GetNameFromUserIdAsync(v.key)
                if name ~= nil then
                    local Val  = v.value
                    local NewObj = game.ServerStorage:WaitForChild("TemplateCoins"):Clone()
                    NewObj:WaitForChild("Player").Text = name
                    NewObj:WaitForChild("Coins").Text = format(Val) 
                    NewObj:WaitForChild("Stats").Text = i.."°"
                    NewObj:WaitForChild("PlayerIcon").Image = "https://www.roblox.com/headshot-thumbnail/image?userId="..(v.key).."&width=150&height=150&format=png"
                    local boards = workspace:WaitForChild("LeaderboardStuff")
                    local sellected = boards:WaitForChild("ViewCoins")
                    NewObj.Position = UDim2.new(0, 0, NewObj.Position.Y.Scale + (0.09 * #sellected:WaitForChild("GUI"):GetChildren()), 0)
                    NewObj.Parent = sellected:WaitForChild("GUI")
                end
            end
        end
    end)
    if not Success then
        error(Err)
    end
    for i=30,1,-1 do
        wait(1)
        Info:WaitForChild("UpdateTime").Text = "Update In("..i..")"
    end
    wait()
end

The value is a Number Value. The Value of it is 44909800000000002968496892153981593868198948444510090605799389908923580416 or 44.9TVg.


Solution

  • Sorry I only just checked this but I have found a Solution. Basically I compress the number down and Save it to the Leader board DataStore with this:

    local Coins = coins ~= 0 and math.floor(math.log(coins) / math.log(1.00000000000001)) or 0
    

    then when I Loop through the DataStore I do UnPack it to its Original Number:

    Val = Val ~= 0 and (1.00000000000001^Val) or 0