Hello!
I recently hired someone to effectively store a decently large amount of values for my game. I’m unsure whether this is the best way of saving these values (image below). This is using Datastore2.
Could this be inefficient? It seems like there could be a far more effective and clean workaround to this.
Here’s how the values are saved individually:
IsNightDefVal = false
local IsNight = SettingValues.IsNight
IsNight.Value = IsNightDefVal
local function IsNightUpdate(UpdatedValue)
IsNight.Value = DS_IsNight:Get(UpdatedValue)
end
IsNightUpdate(IsNightDefVal)
DS_IsNight:OnUpdate(IsNightUpdate)
I’m not a beginner when it comes to scripting, I’m just really inexperienced with Datastore2.
It also spews this out in the Output tab every time I exit the game:
Any of your input would be helpful and greatly appreciated!
Yes. You should be using 1 datastore and using the combine function.
1 Like
The output is irrelevant, it’s because DataStore2 does not save when it is in Studio to avoid messing up a production database.
Here’s your current setup. Each box is a database, and boxes within that box represent keys.
But what’s the problem? Well, you have to get a key from each database every time you want information for a player. This is inefficient because you are way more likely to run into rate limits.
So how can we make this better? Well, we know we can use dictionaries to store multiple key-value pairs in one object. How about we use that in our database?
Well, it turns out we can! We can pack all of our values into a dictionary, then use HttpService:JSONEncode
to serialize it into a string suitable to be put into the database.
Let’s give that a try:
We’ve now reduced our required database retrieval calls from 3 to 1. Yay!
To turn this back into a dictionary, we can use HttpService:JSONDecode
.
3 Likes
You don’t actually need to use JSONEncode.
1 Like
Do you not? I thought you did.
Oh well.
Wow, thanks for the really in-depth response!
I’ve looked into the API for Datastore2 and found you can just Combine all of the values (example):
DataStore2.Combine("DATA", "DetailedIcons","Pride","IsNight")
I haven’t finished testing it yet though, but I truly appreciate such a well-explained answer!
1 Like