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

[SOLVED] Why am I getting the "Cannot store array in datastore" error?

For some reason, when saving, the datastore attempts to store an array, which outputs the following error:

00:15:35.817 - Unable to save for 7z99. Error: 104: Cannot store Array in data store. Data stores can only accept valid UTF-8 characters.

Why, and is there a solution?

Here's my datastore script:

local DS = game:GetService("DataStoreService"):GetDataStore("a")
game.Players.PlayerAdded:Connect(function(player)
	variablePlayer = player.Name
    local folder = Instance.new("Folder",player)
    folder.Name = "inventoryfolder"
    inventory = {}
    inventory.Name = "inventory"
    local dataKey = "userkey_"..player.UserId
    local data
    local success,errormsg = pcall(function()
        data = DS:GetAsync(dataKey)
    end)
    if success then
		if data ~= nil then
			for i,v in pairs(data) do
				print(i,v)
				local i2 = Instance.new("StringValue", folder)
				i2.Value = v
			end
		else
			print("Player is new.")
		end	
	else
        warn("Error when retrieving data for "..player.Name..". Error: "..errormsg)
        folder.Value = nil
    end
    end)
game.Players.PlayerRemoving:Connect(function(player)
    local success,errormsg = pcall(function()
	
        for i, v in pairs(player.inventoryfolder:GetChildren()) do
			table.insert(inventory, i, v.Value) -- Inserts each value from the inventoryfolder folder which is the child of the player.
			print(v.Value)

        end

	DS:SetAsync("userkey_"..player.UserId, inventory) -- Attempts to save the table, but can't.
    end)
    if not success then
        warn("Unable to save for "..player.Name..". Error: "..errormsg)
    end
end)

1 answer

I ended up finding a solution by asking elsewhere.

The solution was creating a temporary table, and adding the values there, and then saving it.

game.Players.PlayerRemoving:Connect(function(player)
local inventory1 = {} -- table to hold data temporarily

for i, v in pairs(player.inventoryfolder:GetChildren()) do
    inventory1[v.Name] = v.Value -- add the value to the table
    print(v.Name, v.Value)
end

    local success,errormsg = pcall(function()
    
    DS:SetAsync("userkey_"..player.UserId, inventory1)

    end)

There were also a few things to remove, they may not have been affecting it, but it's good to remove.

inventory.Name = "inventory" -- This is a table, it doesn't have a name.

folder.Value = nil -- This is a folder and doesn't have a value property.

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