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.

01function saveInventory(inventoryData,player,savedData)
02    inventoryDataStore:SetAsync(player.UserId,inventoryData)
03    savedData = inventoryDataStore:GetAsync(player.userId)
04    print(savedData)
05    for i, v in ipairs(savedData.inventoryData) do
06        print(v)       
07    end
08end
09 
10function fillTable(player,inventoryData)
11 
12    print("--------------")
13    inventoryContent = player.inventory.inventoryItemFolder:GetChildren()
14 
15    for i, child in ipairs(inventoryContent) do
View all 31 lines...

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.

01function saveInventory(inventoryData,player,savedData)
02    local success,err = pcall(function()
03        inventoryDataStore:SetAsync(player.UserId,inventoryData)
04        savedData = inventoryDataStore:GetAsync(player.userId)
05    end)
06    if success then
07        print(savedData)
08        for i, v in ipairs(savedData) do --Instead of looping through savedData.inventoryData I just set it to loop through savedData
09            print(v)       
10        end
11    else
12        warn("Datastore failure: "..err)
13    end
14end
15 
View all 37 lines...
Ad

Answer this question