How do you store a collection of objects in a data store dictionary?
I'm currently attempting to make an adventure game in Roblox; a one like Roblox Adventure 3: Rise of Nobeesi. I'm not really having problems (except trying to make a level fun) except for the DataStore. Previously, it was totally broken, because the thing didn't save/load-- but I fixed it. Unfortunately, there's still a glitch in saving. Loading works; but saving doesn't (i tested it by setting the data manually in the command bar). I found out that saving doesn't work because it can't store dictionaries inside the data store dictionary. How did Polyhex did it? Maybe he had to write like a billion items to check whether a certain Token was collected, but I don't want to do that; it's too complicated and long. How do I store a collection of objects in a DataStore dictionary, then?
For extra proof and comprehension, here is my DataStoreService module script:
003 | local DataStoreService = game:GetService( 'DataStoreService' ) |
004 | local playerData = DataStoreService:GetDataStore( 'PlayerData' ) |
005 | local sessionData = { } |
007 | local function retry(func) |
008 | local s, data, tries = true , nil , 0 |
011 | s, msg = pcall ( function () |
015 | if not s then wait( 1 ) end |
016 | until tries = = 3 or s |
019 | warn( 'Datastore failure: ' .. msg) |
025 | local function setStats(player) |
026 | local data = sessionData [ player ] |
027 | local PlayerData = game.ReplicatedStorage.PlayerData [ player.Name ] |
028 | data.stars = PlayerData.Stars.Value |
029 | data.coins = PlayerData.Coins.Value |
030 | data.weapons = PlayerData.Weapons:GetChildren() |
031 | data.collectedStars = PlayerData.CollectedStars:GetChildren() |
034 | local function save(player) |
035 | return retry( function () |
036 | return playerData:SetAsync(player.UserId, sessionData [ player ] ) |
040 | local function load (player) |
041 | return retry( function () |
042 | return playerData:GetAsync(player.UserId) |
046 | local function autosave() |
048 | for player, data in pairs (sessionData) do |
055 | local function loadToGame(player) |
056 | local data = sessionData [ player ] |
057 | local PlayerData = game.ReplicatedStorage.PlayerData [ player.Name ] |
058 | PlayerData.Stars.Value = data.stars |
059 | PlayerData.Coins.Value = data.coins |
060 | table.foreach(data.weapons, function (_, child) |
061 | child:Clone().Parent = PlayerData.Weapons |
062 | child:Clone().Parent = player:WaitForChild( 'Backpack' ) |
064 | table.foreach(data.collectedStars, function (_, child) |
065 | child:Clone().Parent = PlayerData.CollectedStars |
069 | local function setup(player) |
070 | local s, data = load (player) |
072 | sessionData [ player ] = false |
075 | sessionData [ player ] = data |
077 | print ( 'Loaded data for ' ..player.Name) |
079 | sessionData [ player ] = { stars = 0 , coins = 0 , collectedStars = { } , weapons = { } } |
080 | print ( 'No data found, creating data for ' ..player.Name) |
085 | game.Players.PlayerAdded:Connect(setup) |
087 | game.Players.PlayerRemoving:Connect( function (player) |
090 | print (sessionData [ player ] .coins) |
093 | sessionData [ player ] = nil |
098 | print ( 'DataStoreService module loaded.' ) |