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

Cannot store Array in DataStore?

Asked by
NexeusX 137
8 years ago
Edited 8 years ago

Ok so i have been working on this script, trying to make it save the children in the Weaps model and then load it into the model when the player enters, everything looks like it would work but when i test it the output says "Cannot store Array in DataStore" or somthing? Can somone tell me what that means and how to fix it?

local datastore = game:GetService("DataStoreService"):GetDataStore("RobloxBackPack")
-------------------------------------------------------------------------

local wm = Instance.new("Model")
wm.Name = "Weaps"

function give(weap, player)

    for _, w in ipairs(game.StarterPack:GetChildren()) do
        if w.Name == weap.Name then return end
    end

    weap:clone().Parent = player.Weaps
end

game.Players.ChildAdded:connect(function(p)
    wm:clone().Parent = p
    wait()
    p.Character.ChildAdded:connect(function(wep)
        if wep.className == "Tool" then
            give(wep, p)
        end
    end)
    p.Changed:connect(function(pr)
        if pr == "Character" then
            for _, w in ipairs(p.Weaps:GetChildren()) do
                w:clone().Parent = p.Backpack
            end
            p.Character.ChildAdded:connect(function(wep)
                if wep.className == "Tool" then
                    give(wep, p)
                end
            end)
        end
    end)
    p.Backpack.ChildAdded:connect(function(h)
        if h.className == "HopperBin" then
            give(h, p)      
        end
    end)
end)



game.Players.PlayerAdded:connect(function(player) --Datastore stuff
    local key = "user-" .. player.userId
    local storeditems = datastore:GetAsync(key)
    if storeditems then
        local Special = storeditems[1]
        Special.Parent = player.Weaps

    else
        local get = player.Weaps:GetChildren()
        for i, v in pairs(get)do
        local Potato = v:clone()
        local items = {Potato}
        datastore:SetAsync(key, items)
        end
    end
end)


--Says it cannot save a data array?



game.Players.PlayerRemoving:connect(function(player) -- more datastore stuff
    local get = player.Weaps:GetChildren()
    for i, v in pairs(get)do
    local Potato = v:clone()

    local items = {Potato}
    local key = "user-" .. player.userId

    datastore:SetAsync(key, items)

    end
end)

2 answers

Log in to vote
1
Answered by 8 years ago
Edited 8 years ago

You cannot store objects in the data store, you will have to save a value which represents the item.

Secondly you are also overwriting the data saved as the SetAsync. "Sets the value of the key. This overwrites any existing data stored in the key. "

To save the data you will need either a key value table or an array. More info can be found here.

As you are loading the data as an array e.g.

local Special = storeditems[1]

Then you will need to save the data as an array:-

game.Players.PlayerRemoving:connect(function(player) -- more datastore stuff
     local key = "user-" .. player.userId
    local items = {} -- our array of data

    for i, v in pairs(player.Weaps:GetChildren()) do
        -- e.g tool name is sword so we save a 1 ect
    local item = [some method to turn the object to a value e.g. string or int]
    table.insert(items, item) -- adds this value to out items list
    end

datastore:SetAsync(key, items) -- once all items have been added save the array list
end)

When loading the data simply convert the numbers back into the tools ect which the player had.

Hope this helps.

Ad
Log in to vote
1
Answered by
Valiux 27
8 years ago
Edited 8 years ago

You are trying to save a model, Potato is a model.

0
I am trying to GetChildren() out of model and then save it to datastore then load it back NexeusX 137 — 8y

Answer this question