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

Cannot store Array in DataStore?

Asked by 8 years ago

I was searching around for this error, found another answer on scriptinghelpers, but the fix the guy gave didn't work for me. I get the error 17:48:16.066 - Cannot store Array in DataStore 17:48:16.067 - Script 'Workspace.Script', Line 49 17:48:16.068 - Stack End" Note: That is not the actual data store name..

local DSService = game:GetService('DataStoreService'):GetDataStore("Name") 
game.Players.PlayerAdded:connect(function(plr)
    local uniquekey = 'id-'..plr.userId
    local leaderstats = Instance.new('IntValue', plr)
    local savevalue =  Instance.new('IntValue')
    local SaveFolder = Instance.new('Folder')
    local SaveStone = Instance.new('IntValue')
    local SaveClay = Instance.new('IntValue')
    local SaveBrick = Instance.new('IntValue')
    local SaveRawIron = Instance.new('IntValue')
    local SaveIron = Instance.new('IntValue')
    leaderstats.Name = 'leaderstats'
    savevalue.Parent = leaderstats
    savevalue.Name = 'Cash'
    SaveFolder.Name = "SaveFolder"
    SaveFolder.Parent = plr
    SaveStone.Name = "SaveStone"
    SaveStone.Parent = SaveFolder
    SaveClay.Name = "SaveClay"
    SaveClay.Parent = SaveFolder
    SaveBrick.Name = "SaveBrick"
    SaveBrick.Parent = SaveFolder
    SaveRawIron.Name = "SaveRawIron"
    SaveRawIron.Parent = SaveFolder
    SaveIron.Name = "SaveIron"
    SaveIron.Parent = SaveFolder
    local GetSaved = DSService:GetAsync(uniquekey)
    if GetSaved then
        savevalue.Value = GetSaved[1]
        SaveFolder = GetSaved[2]
        SaveStone.Value = GetSaved[3]
        SaveClay.Value = GetSaved[4]
        SaveBrick.Value = GetSaved[5]
        SaveRawIron.Value = GetSaved[6]
        SaveIron.Value = GetSaved[7]
    plr.PlayerGui.Invintory.Inv.Stone.Text = "Stone: "..SaveStone.Value
    plr.PlayerGui.Invintory.Inv.Clay.Text = "Stone: "..SaveClay.Value
    plr.PlayerGui.Invintory.Inv.Bricks.Text = "Stone: "..SaveBrick.Value
    plr.PlayerGui.Invintory.Inv.RawIron.Text = "Stone: "..SaveRawIron.Value
    plr.PlayerGui.Invintory.Inv.Iron.Text = "Stone: "..SaveIron.Value
    else
        local NumbersForSaving = {savevalue.Value,
                                  SaveFolder,
                                  SaveStone.Value,
                                  SaveClay.Value,
                                  SaveBrick.Value,
                                  SaveRawIron.Value,
                                  SaveIron.Value}
        DSService:SetAsync(uniquekey, NumbersForSaving)
    end
end)

game.Players.PlayerRemoving:connect(function(plr)
local uniquekey = 'id-'..plr.userId
local Savetable = {plr.leaderstats.Cash.Value,
                   plr.SaveFolder,
                   plr.SaveFolder.SaveStone.Value,
                   plr.SaveFolder.SaveClay.Value,
                   plr.SaveFolder.SaveBrick.Value,
                   plr.SaveFolder.SaveRawIron.Value,
                   plr.SaveFolder.SaveIron.Value}
DSService:SetAsync(uniquekey, Savetable)
end)
0
JsonEncode it. Scarious 243 — 8y
0
How do I do that? dotProgram 0 — 8y
0
Answered... took 20 minutes but yw. Scarious 243 — 8y

1 answer

Log in to vote
0
Answered by
Scarious 243 Moderation Voter
8 years ago
Edited 8 years ago

You can only have one (1) type of statistic stored in a table in a datastore...

plr.SaveFolder cannot be saved, because it is not the same type of object as plr.SaveFolder.SaveStone.Value

Solution?

JSONEncoding, and JSONDecoding...

To do JSONEncoding:

http://wiki.roblox.com/index.php?title=API:Class/HttpService/JSONEncode

Why is this useful? You can convert data into Strings.

But how do I do this? First, you must access the HttpService, this does not mean HttpService needs to be enabled though:

HttpService = game:GetService("HttpService")

Now, you will give the table you want to encode:

local atable = {"A","B","a","b"}

Since you have this, you can now begin to encode it...

local encoding = HttpService:JSONEncode(atable)
print(encoding)

Now, you have a string.

http://wiki.roblox.com/index.php?title=API:Class/HttpService/JSONDecode

When retrieving the data given to the datastore, you must now decode the encoding... You can go about this by using the JSONDecode("AString") event...

local decoding = HttpService:JSONDecode(encoding)

Now you have a table... hooray.

As for your script, now that I have explained this... will go as follows:

HttpService = game:GetService("HttpService") -- Put this at the start of the script.

local EncodedTable = DSService:GetAsync(uniquekey)
local DecodedTable
if EncodedTable then
    DecodedTable = EncodedTable:JSONDecode(EncodedTable)
end
if DecodedTable ~= nil then
    savevalue.Value = DecodedTable[1]
    SaveFolder = DecodedTable[2]
    SaveStone.Value = DecodedTable[3]
    SaveClay.Value = DecodedTable[4]
    SaveBrick.Value = DecodedTable[5]
    SaveRawIron.Value = DecodedTable[6]
    SaveIron.Value = DecodedTable[7]
    plr.PlayerGui.Invintory.Inv.Stone.Text = "Stone: "..SaveStone.Value
    plr.PlayerGui.Invintory.Inv.Clay.Text = "Stone: "..SaveClay.Value
    plr.PlayerGui.Invintory.Inv.Bricks.Text = "Stone: "..SaveBrick.Value
    plr.PlayerGui.Invintory.Inv.RawIron.Text = "Stone: "..SaveRawIron.Value
    plr.PlayerGui.Invintory.Inv.Iron.Text = "Stone: "..SaveIron.Value
else
    local NumbersForSaving = {
        savevalue.Value,
        SaveFolder,
        SaveStone.Value,
        SaveClay.Value,
        SaveBrick.Value,
        SaveRawIron.Value,
        SaveIron.Value}
    local EnocdedNFS = HttpService:JSONEncode(NumbersForSaving)
    DSService:SetAsync(uniquekey,EncodedNFS)
end

That's for the PlayerAdded, for the PlayerRemoving:

game.Players.PlayerRemoving:connect(function(plr)
local uniquekey = 'id-'..plr.userId
local Savetable = {plr.leaderstats.Cash.Value,
                   plr.SaveFolder,
                   plr.SaveFolder.SaveStone.Value,
                   plr.SaveFolder.SaveClay.Value,
                   plr.SaveFolder.SaveBrick.Value,
                   plr.SaveFolder.SaveRawIron.Value,
                   plr.SaveFolder.SaveIron.Value}
local EncodedSt = HttpService:JSONEncode(Savetable)
DSService:SetAsync(uniquekey, EncodedSt)
end)

This should do it for you... There could be errors, it was done in the Tab for this.

-Taryo


Thgirypoc (C) 6102 Oyrat

0
When I changed everything out, I am now getting a blue underline under DSService:SetAsync(uniquekey,EncodedNFS) @ line 57 it says "W001 Unknown Global "EncodedNFS"" dotProgram 0 — 8y
0
And when I load the game I get 2 new erros 18:41:26.315 - Place has to be opened with Edit button to access DataStores 18:41:26.320 - Workspace.Script:29: attempt to index upvalue 'DSService' (a nil value) dotProgram 0 — 8y
0
Oh, the second error is because you must go to roblox develop page, and then press edit on the game to use datastore system... assuming you have studio api access enabled... which is another thing. Scarious 243 — 8y
0
Also, if DSService is a nil value... you couldn't have used it in your previous answer.. you should still have your datastoreservice. Scarious 243 — 8y
Ad

Answer this question