Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
0

Converting 2 datastores into 1?

Asked by 5 days ago
Edited 5 days ago

So I have a game and it saves 2 number values. You see, I was ignorant enough to not realise I should save both values into one datastore, so I saved the 2 values in separate datastores. Now, when I need to save something else, it overflows and doesn't save. So, how can I fuse the 2 datastores into 1 and save the values onto a dictionary, which saves to the new datastore. I can't just change the datastore or else everyone would lose their data. So, how can I do this?

Get Data Script

Players.PlayerAdded:Connect(function(client)
    local moneyretries = 3
    local inventoryretries = 3
    local bountyretries = 3
    local codesretries = 3
    local key = "client_" .. client.UserId

    repeat
        inventory = {}
        local succ, inventorytable = pcall(function()
            return player_data:GetAsync(key)
        end)
        inventory = inventorytable
        inventoryretries = inventoryretries - 1
        wait(.1)
    until moneyretries == 0 or succ
    repeat
        money = 0
        local succ, moneyvalue = pcall(function()
            return money_data:GetAsync(key)
        end)
        money = moneyvalue
        moneyretries = moneyretries - 1
        wait(.1)
    until moneyretries == 0 or succ

    repeat
        bounty = 0
        local succ, bountyvalue = pcall(function()
            return bounty_data:GetAsync(key)
        end)
        bounty = bountyvalue
        bountyretries = bountyretries - 1
        wait(.1)
    until bountyretries == 0 or succ

    repeat
        codes = {}
        local succ, codesTable = pcall(function()
            return codes_data:GetAsync(key)
        end)
        codes = codesTable
        codesretries = codesretries - 1
        wait(.1)
    until codesretries == 0 or succ

    local inventory_folder = Instance.new("Folder")
    inventory_folder.Name = client.Name
    inventory_folder.Parent = inventories

    local codes_folder = Instance.new("Folder")
    codes_folder.Name = client.Name
    codes_folder.Parent = ServerStorage.Codes

    client:WaitForChild("leaderstats"):WaitForChild("Money").Value = money
    client:WaitForChild("leaderstats"):WaitForChild("Bounty").Value = bounty

    for _, name in pairs(inventory or { }) do
        local tool = tools[name]
        tool:Clone().Parent = inventory_folder -- For saving and loading
    end

    for _,code in pairs(codes or { }) do
        local code = codesRep[code]
        code:Clone().Parent = codes_folder
    end

    inventoryretries = 3
    moneyretries = 3
    bountyretries = 3
    codesretries = 3

    warn("finshed loading data")
end)

Saving Data Script

Players.PlayerRemoving:Connect(function(client)
    local moneyretries = 3
    local bountyretries = 3
    local inventoryretries = 3
    local codesretries = 3
    local key = "client_" .. client.UserId
    local tools = { }
    local codes = { }
    local money
    local bounty
    local codes_folder = ServerStorage.Codes[client.Name]
    local inventory_folder = inventories[client.Name]

    for _, item in ipairs(inventory_folder:GetChildren()) do
        table.insert(tools, item.Name)
    end

    for _, code in ipairs(codes_folder:GetChildren()) do
        table.insert(codes, code.Name)
    end

    repeat
        local succ, inventory = pcall(function()
            player_data:UpdateAsync(key, function(prev)
                return tools
            end)
        end)
        inventoryretries = inventoryretries - 1
        wait(.1)
    until inventoryretries == 0 or succ

    repeat
        local succ, codes = pcall(function()
            player_data:UpdateAsync(key, function(prev)
                return codes
            end)
        end)
        codesretries = codesretries - 1
        wait(.1)
    until codesretries == 0 or succ

    money = client.leaderstats.Money.Value
    bounty = client.leaderstats.Bounty.Value

    repeat
        local succ, money = pcall(function()
            money_data:UpdateAsync(key, function(prev)
                return money
            end)
        end)

        moneyretries = moneyretries - 1
        wait(.1)
    until moneyretries == 0 or succ

    repeat
        local succ, bounty = pcall(function()
            bounty_data:UpdateAsync(key, function(prev)
                return bounty
            end)
        end)
        bountyretries = bountyretries - 1
        wait(.1)
    until bountyretries == 0 or succ

    inventory_folder:Destroy()
    codes_folder:Destroy()

    inventoryretries = 3
    moneyretries = 3
    codesretries = 3
    bountyretries = 3
    warn("datastore save finished")
end)

Answer this question