Can you datastore dictionaries?

Recently , I’ve been wanting to create a game which requires Datastore, but the only practice I have using datastore is saving values like BoolValues or NumberValues, however I heard that these value objects are highly exploitable so I wanted to switch to a new was of storing the players data.

For example:

Say I wanna keep a record of if someone bought a developer product or the new “premium Up sale” thing,

local Storage = {

BoughtPremium = true,
Bought30DayVip = false
... ect ect
}

Can you data store these values? and if so, would anyone be wiling to show me how or link a useful roblox lua site/video?

Thanks :slight_smile:

1 Like

Yes you can just save them like a regular value, all you need to do is what you would normally do to save your datastore but just use your Storage variable instead

YourDataStore:SetAsync(key, Storage)

Let me know if something I said is inaccurate or incorrect.

Yes, by using JSON.

When saving the data do something like:

dataToSave = {
BoughtPremium = game.Players.BoughtPremium.Value
}

local Data = game:GetService("HttpService"):JSONEncode(Storage)
DataStore:SetAsync(player.UserId, Data)

--//And when loading do it like:

local success, err = pcall(function()
	data= DataStore:GetAsync(player.UserId) or game:GetService("HttpService"):JSONEncode(Storage) 
end)

local loadJSON = HttpService:JSONDecode(data)

Then to get the data, you create instances (e.g. Instance.new(“IntValue”)) and then the value of that is loadJSON.BoughtPremium (or whatever the thing you saved was)

Please note: UpdateAsync may be safer to use, but I used SetAsync for the sake of simplicity.

Oh wow I never actually thought to use JSON, Ive never even used that before so I’ll have to look into that

I don’t believe you need to use JSON to save your tables

2 Likes

I’m pretty sure Roblox converts your tables into JSON to store them in the first place. You don’t have to do this step on your own, just make sure not to mix string and integer keys

Yeah you’re correct on that. It auto encodes and auto decodes it, just needed to refresh my memory.

1 Like

What do you mean by “dont mix string and integer keys”?

So I can’t have like:

local storage = {

Coins = 0,
Vip = false,
PremiumBought = true
}
?

You can! I mean you cant do something like this:

local playerInventory = {
    "bow",
    "sword",
    "stinky boot",

    coins = 100
}

This cannot be converted to JSON (Nor is it very good practice to do anyways, but I’ve seen it a few times)

Where something like this can be converted:

local playerInventory = {
    items = {
        "bow",
        "sword",
        "stinky boot",
    },
    coins = 100,
}

Basically you gotta keep arrays and dictionaries separated

1 Like

They do? Damn, that’s kinda cool. Wish I’d known about that sooner haha

Awesome good to know :slight_smile: thanks guys

2 Likes