How to insert values into table within datastore?

So, I am trying to make a game where each level you complete it gets added to a datastore table. The reason I need a table within a datastore and not multiple datastores is because to get to each level you use a GUI that relies on only getting async once so it doesn’t interact with the datastore beyond the limits.

My issue is I have no idea how to add values into a table that is stored on a datastore. Without it resetting all data from future levels (players may want to go back to previous levels)

Here is one of the solutions I’ve made:

success, response = pcall(function()
				datastore:UpdateAsync(player.UserId.."-DataTable", function(oldValue)
					if oldValue.type(table) then
						oldValue["test"] = true
					end

					return oldValue
				end)
			end)

			if success then
				print('Updated player data!')
			else
				warn(response)
			end

You could potentially use a more reliable way of loading/storing data.

Something I personally like to do is create Folders that hold player data and that can be used as the player data holder.

To save/load data I just serialize my instances into Arrays and save it to the datastore and to load it I would do the same vise versa.

So would I serialize a folder of BoolValues? So I can store which levels are unlocked or locked?

You could definetely do that!

Or alternatively, you can create a StringValue for every level that gets unlocked, store those values in a folder which will then be converted into an array and you can simply save the names of said StringValues.

ah, so making sure I understand. I make a StringValue that is named for each level that is unlocked, serialize that into an array to send to the DataStore which I can then deserialize? Also, I don’t need to save anything in the value of the StringValue correct?

I’ve figured out why my script wasn’t working. It didn’t pass the if oldValue.type(table) check, but without it my script works perfectly.

2 Likes