How should I store an array in a datastore?

Hey guys! So I was having a problem with Datastores in creating my loadout saving mechanic in my game, and it says I can’t use Arrays in Datastores. I’m not sure how I should go about putting the array into the datastore. Anyone have ideas? My code that I have is below in case you need to know what I’m doing

local DatastoreService = game:GetService("DataStoreService")
local Remote = game.ReplicatedStorage.Hotbar

local Hotbar = DatastoreService:GetDataStore("Hotbar")

Remote.OnServerEvent:Connect(function(p,a,b,c,d,e)
	Hotbar:SetAsync(p.UserId, {a,b,c,d,e})
end)

You could just make it as a dictionary, every key is indexed using a number.

local dictionary = {
    1 = a,
    2 = b,
    3 = c
}
1 Like

This is a pretty undescriptive error, it means that something in the array can’t be saved. What is a, b, c, d, and e?

The error literally said arrays cannot be saved through data store service.

2 Likes

Okay so for context, I’m making a tower defense game and I’m trying to implement allowing you to customize what you start the game with (towers to play with) and each slot in the inventory has a value, (theres 5 slots) and they all have a value that simply just says the name of the tower.

example: a is = soldier, b is gladiator, etc…

so like this?

local dictionary = {
	1 = a,
	2 = b,
	3 = c,
	4 = d,
	5 = e
}

Yes, they can. Your script is literally the exact same thing as what he’s doing.

If those soldier and gladiator are just instances, then it’s the problem.

they are not, I put a stringvalue inside the slots to actually send the data over

Yes, you can just use key as numbers to “act” as like an array.

Then it’s the format issue of saving the data. In simple words, arrays cannot be saved through data store.

I’m getting an error with the dictionary thing saying
image

edit: should I be sending the dictionary thru the event from the localscript?

Then I’m really not sure, but the only idea I have left is just use a key to index an array of your troops.

local dataToSave = {
    Troops = {
        a,
        b,
        c,
        d
    },
    Money = …
}

Okay, well thank you for the help!

I changed the dictionary to use text values instead of numbers, and this works:

local dictionary = {
	slot1 = tostring(Slot1.Value),
	slot2 = tostring(Slot2.Value),
	slot3 = tostring(Slot3.Value),
	slot4 = tostring(Slot4.Value),
	slot5 = tostring(Slot5.Value)
}

I don’t know if the tostring was necessary

I personally take the complicated method and HttpService:JSONEncode() everything that involves inventories, past player setting preferences, etc.

If I’m not incorrect, if you don’t use HttpService:JSONEncode() you’ll have issues storing Strings + Numbers in an array.

This is incorrect.

JSON encryption is not a necessity for data storage. Not that it is useless, but you can store table types either way.


There is a misunderstanding of tables in this thread. Dictionaries are an extension of arrays. You can store a dictionary in a DataStore, and likewise you can store an array in a DataStore at less cost. Our games do it consistently with no external modules.

image

OP, I’m not sure why that error is throwing, but I would suspect it is the data types that you are attempting to save.

Oh, I’ve experienced an issue in the past where my code printed an error although I was able to do it before, where it said “Cannot store Dictionary in DataStore” and my old and newer script were relatively the same, utilizing only numbers + strings. No instances because you cannot save those to a DataStore.

The minute I switched over to HttpService:JSONEncode() the error immediately went away so I assumed it was the mix of numbers and such.

Thanks for the elaboration though.

I’m not an expert at anything under the hood of Luau. You will see certain improvements from encryption likely because I imagine less bytes are needed.

Ages ago I would write my Data with encryption, now my primary use-cases for JSON involve actual Http requests.

1 Like