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

Is it possible to store a value and it's children in a datastore?

Asked by 1 year ago
Edited 1 year ago

To make a long story short, I'm wondering if it's possible to save, specifically, the children of a value (IntValue) in a DataStore. For example, if you have a value called "Money" and it has a child called "Quarter" secondarily added to it, Quarter would become part of the datastore and would save across play sessions. Here is a method I tried to use to do this, although I get an error saying that Arrays can not be saved in datastore.

DS = game:GetService("DataStoreService")
DataStore = DS:GetDataStore("PlayerCurrencies")
local ServerStorage = game:GetService('ServerStorage')

local RS = game:GetService("RunService")



game.Players.PlayerAdded:Connect(function(player)
    local Money = Instance.new('IntValue')
    Money.Name = 'Money'
    Money.Parent = player

    local Children = Money:GetChildren()


    local Inventory = Instance.new('Folder', player)
    Inventory.Name = player.UserId

    local data
    local success, err = pcall(function()
        data = DataStore:GetAsync('playerdata_'..player.UserId)
    end)


    if success then
        print('sucess in set async')
        if data then

            print('data collected')

            Money.Value = data[1] 

            for i, Child in pairs(Children) do
                Child.Parent=Money
            end



        end





    end

end)


game.Players.PlayerRemoving:Connect(function(player)

    local Money = player.Money
    local Children = Money:GetChildren()
    local ChildTable = {}

    for i, Child in pairs(Children) do
        table.insert(ChildTable, Child)
    end

    local data = {Money.Value, ChildTable}

    local success, err = pcall(function()
        DataStore:SetAsync('playerdata_'..player.UserId, data)
    end)

    if success then
        print('data has saved'..Money.Value..ChildTable)
    else
        warn(err)
        print(Children)
        print(ChildTable)
    end
end)


game:BindToClose(function()
    wait(2)
end)

If this is unfixable, is there any alternate methods I could try to use? Thank you.

1 answer

Log in to vote
0
Answered by
CDXKIE 130
1 year ago

I'm not sure what player.Money is, but because of you using :GetChildren() I'm going to assume it's a folder.

You can't store Instances, (aka objects), so I think the problems is in:

table.insert(childTable, child)

What I suggest doing, is inserting the child's name, and then having a folder of all possible money, (again, I don't know what the money folder is, so that may be impossible, if so it's time to rewrite your money system.) and when you're retrieving the data, do:

local money = folderOfAllPossibleMoneyObjects:FindFirstChild(childName)
if money then
    money:Clone.Parent = player.Money
end

If you provided more information on what player.Money is, I'd be able to help you further.

Ad

Answer this question