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

Struggling to access data store variable?

Asked by 5 years ago

I'm trying to write a pretty basic inventory store system. At the moment it takes all the items from the player's inventory folder, stores the names of the items in a table and saves them (when the player leaves). However, when trying to print the data the error I get is:

ServerScriptService.InventorySaveScript:31: bad argument #1 to 'ipairs' (table expected, got nil) This error is caused on line 05.

If you're trying to interpret the script, start with the player removing function.

function saveInventory(inventoryData,player,savedData)
    inventoryDataStore:SetAsync(player.UserId,inventoryData)
    savedData = inventoryDataStore:GetAsync(player.userId)
    print(savedData)
    for i, v in ipairs(savedData.inventoryData) do
        print(v)        
    end
end

function fillTable(player,inventoryData)

    print("--------------")
    inventoryContent = player.inventory.inventoryItemFolder:GetChildren()

    for i, child in ipairs(inventoryContent) do
        table.insert(inventoryData, i, child.Name)
    end

    if inventoryData ~= nil then
        for i, v in ipairs(inventoryData) do
            print(v)        
        end
    end

    saveInventory(inventoryData,player)

end

game.Players.PlayerRemoving:Connect(function(player)
    fillTable(player,inventoryData)
end)

1 answer

Log in to vote
0
Answered by 5 years ago

Well from reading your script it seems that when you try to loop through the data you are indexing something that isn't there. On line 5 you try to index savedData.inventoryData as if savedData is a dictionary but I don't recall you ever setting a table inside the datastore called inventoryData.

I might not be correct but your problem might be fixed if you just try to index savedData not savedData.inventoryData.

I have done a few changes to your script just to improve it. It is generally a good idea to wrap any datastore functions in a pcall so that if it doesn't work your script won't break.

function saveInventory(inventoryData,player,savedData)
    local success,err = pcall(function()
        inventoryDataStore:SetAsync(player.UserId,inventoryData)
        savedData = inventoryDataStore:GetAsync(player.userId)
    end)
    if success then
        print(savedData)
        for i, v in ipairs(savedData) do --Instead of looping through savedData.inventoryData I just set it to loop through savedData
            print(v)        
        end
    else
        warn("Datastore failure: "..err)
    end
end

function fillTable(player,inventoryData)

    print("--------------")
    local inventoryContent = player.inventory.inventoryItemFolder:GetChildren()

    for i, child in ipairs(inventoryContent) do
        table.insert(inventoryData, i, child.Name)
    end

    if inventoryData ~= nil then
        for i, v in ipairs(inventoryData) do
            print(v)        
        end
    end

    saveInventory(inventoryData,player)

end

game.Players.PlayerRemoving:Connect(function(player)
    fillTable(player,inventoryData)
end)
Ad

Answer this question