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

[Solved]Could someone help me understand how to save all three items into one datastore?

Asked by 5 years ago
Edited 5 years ago

Here is my code:

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

    if plr.CanSave.Value then

        local specialKey = "User_"..plr.UserId
        local plrName = plr.Name
        local plrInfo = game.ServerStorage:FindFirstChild(plrName):WaitForChild("PlayerInfo")

        if plrInfo.OwnsTycoon.Value then

            local tycoonName = plrInfo.OwnedTycoon.Value
            local tycoon = game.Workspace.Tycoons:FindFirstChild(tycoonName)
            local moneyToSave = plrInfo.Money.Value
            local buildLevelToSave = plrInfo.BuildLevel.Value
            local purchasedItemsTable = getTableForDataStore(tycoon, plrInfo)
            local mainTableToSave = {moneyToSave, buildLevelToSave, purchasedItemsTable}

            local success, message = pcall(function()

                local encodedData = hService:JSONEncode(mainTableToSave)
                 mainDataStore:SetAsync(specialKey, encodedData) -- here is what I tried

            end)

            if success then

                print(plrName.."'s data saved successfully")

            else

                print(plrName.."'s data failed to save successfully")

                wait(15)

                local maxAttemptsAllowed = 3
                local saveAttempts = 0

                repeat

                    saveAttempts = saveAttempts + 1

                    local success, message = pcall(function()

                        local encodedData = hService:JSONEncode(mainTableToSave)
                        mainDataStore:SetAsync(specialKey, encodedData)

                    end)

                    if success then

                        print(plrName.."'s data saved successfully on try "..saveAttempts)

                    else

                        print(plrName.."'s data failed to save successfully on try "..saveAttempts)                     

                    end

                until success or saveAttempts == maxAttemptsAllowed

            end
        end 
    end
end)

Answer this question