Error in Ordered Data Store

Hey guy’s I am scripting a global leaderboard for my game but I ran into this error:

I can’t seem to see what the problem is

local DataStoreService = game:GetService("DataStoreService")
local GlobalLeaderboard = DataStoreService:GetOrderedDataStore("GlobalLeaderboard.1")
local RunService = game:GetService("RunService")
local leaderboard = {}
leaderboard.__index = leaderboard

function leaderboard:Refresh()
    local Model = self.Model
    local List = Model:FindFirstChild("Items",true)

    local Time = os.time()

    RunService.Heartbeat:Connect(function(deltaTime)
        if os.time() - Time >= 5 then
            Time = os.time()

            for _, v in pairs(List:GetChildren()) do
                if v:IsA("Frame") then
                    v:Destroy()
                end
            end

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

            --local success, err = pcall(function()
                local data = GlobalLeaderboard:GetSortedAsync(false,10)--GetSortedAsync(false,10)
                local page = data:GetCurrentPage()

                for Rank,SavedData in pairs(page) do
                    local Username = game.Players:GetNameFromUserIdAsync(tonumber(SavedData.key))
                    local Cash = SavedData.value

                    local New = Instance.new("TextLabel")
                    New.Size = UDim2.new(1,0,.1,0)
                    New.Font = Enum.Font.SourceSansBold
                    New.TextScaled = true
                    New.Parent = List
                    New.Name = Username
                    New.Text = "#"..Rank.." "..Username.." "..Cash
                    New.Visible = true
                end
            --end)

            --if not success then
            --    warn(Model.." error:",err)
            --end
        end
    end)
end

function leaderboard.new(Model)
    local self = setmetatable({},leaderboard)

    self.Model = Model

    return self
end

return leaderboard

I get that error all the time when I’m making a leaderboard, you should probably just wrap whatever line that is inside a pcall so it doesn’t error the script. Unless there is a genuine fix to this, if anyone has one then I would love to know it.

To add on to what @beatpeat said, all network calls, such as :GetAsync() and :SetAsync(), have the potential to error and should be wrapped in a pcall() to catch any errors and prevent crashing the script.

Go into studio settings > Security > Allow studio access to API service and turn it on. The error should go away and data should save.

I don’t understand why this is giving an error still though