Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Struggling to access data store variable?

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

✓ Accepted answer

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)

This discussion is preserved as it was. New answers, comments, and votes are closed.