Global Leaderboard Error (API and Must be Integer numbers)

I was writing a Global LeaderBoard code, but I encountered some errors. Could you help? The error occurs at line 10.

Error1
DataStoreService: ValueNotNumeric: Sorted datastore values must be integer numbers API: SetAsync, Data Store: PowerLeaderboard 
Error2
502: API Services rejected request with error. Error code: 14 Reason: Sorted datastore values must be integer numbers
local DataStoreService = game:GetService("DataStoreService")
local PowerDataStore = DataStoreService:GetOrderedDataStore("PowerLeaderboard")

local LeaderboardPart = game.Workspace.PowerLeaderBoard
local RefreshRate = 5

local function RefreshLeaderboard()

    for i, Player in pairs(game.Players:GetPlayers()) do
        PowerDataStore:SetAsync(Player.UserId, tonumber(Player.leaderstats.Power.Value))
    end

    local Success, Error = pcall(function()

        local Data = PowerDataStore:GetSortedAsync(false, 10)
        local PowerPage = Data:GetCurrentPage()

        for Rank, SavedData in ipairs(PowerPage) do

            local Username = game.Players:GetNameFromUserldAsync(tonumber(SavedData.Key))
            local Power = SavedData.Value

            if Power then
                local NewSample = game.ReplicatedStorage.LeaderBoard:Clone()
                NewSample.Parent = LeaderboardPart.SurfaceGui.ScrollingFrame
                NewSample.Name = Username
                NewSample.Power.Text = Power
            end
        end
    end)
end

while true do
    for i, Frame in pairs(LeaderboardPart.SurfaceGui.ScrollingFrame:GetChildren()) do
        if Frame:IsA("Frame") then
            Frame:Destroy()
    end
    RefreshLeaderboard()
    wait(RefreshRate)
end

If Player.leaderstats.Power.Value is a very large number, just round it with math.round before trying to put it into the DataStore.

However, if the fractional value matters to you that much, you can always save it to a certain degree of precision by multiplying the value by 10, 100, etc before rounding it, then just divide it by that same number when you get it from the DataStore.

2 Likes

Unfortunately, I need to save very large numbers, how do I solve this?

I am running into this same problem. Roblox is returning scientific notation instead of an integer with very large numbers and that scientific notation cannot be stored to ordered datastores. Does anyone have a solution for this?

I solved the problem by calculating the power of 10 of the number. For example, let’s say this number is 4,586,800. This can be written as 4.586x10^6. In other words, when saving a number, we need to save both the lesser form (4.586) and the power of 10 (6) form. I hope this helps.