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

Why is this outputting table expected got nil?

Asked by 5 years ago

I'm playing around with Datastores and the following code works fine but after the first attempt it errors and says line 13 bad argument #1 to 'pairs' (table expected, got nil).

It works perfectly fine and it updated the data but when I leave and come back for the 2nd time it errors with that. When I tried to print data.stats it gave me a nil value??

local DS = game:GetService('DataStoreService'):GetDataStore('7')

local data
local key = '1'

local success, ret = pcall(function()
    data = DS:GetAsync(key) 
end)

if data then
    local stats = data.stats

    for k, v in pairs(stats) do
        print(k,v)
    end

    wait(1)

    DS:UpdateAsync(key, function(prev)
        stats.gold = not stats.gold
        return stats
    end)

    wait(1)

    for k, v in pairs(stats) do
        print(k,v)
    end
elseif not data then
    DS:SetAsync(key, {
        stats = {
            armor = true;
            gold = false;
            silver = false;
            wood = false;
        };
    })  
else
    error(ret)
end
0
Seriously! What is up with this DataStore flood?? Ziffixture 6913 — 5y

Answer this question