Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

Clearing an OrderedDataStore based on version number?

Asked by
Edenojack 171
8 years ago

In my game I have a speedrunning leaderboard, however I'm constantly fixing bugs. What is a good way of me clearing an OrderedDataStore every time I change the version?

2 answers

Log in to vote
0
Answered by 8 years ago

Store your place version


Personally, I'd store the place version of your game with data store. Once you've stored your place version, you can then compare it to it's current place version to see if it's new. If it is, then create some function that sets all your keys in OrderedDataStore to nil.

Implementation

Implementing this is pretty simple. You can get your game's current version with game.PlaceVersion, and just save it directly to a data store. First, just create your desired data store to save the version number:

1-- Get the data store service
2local datastore = game:GetService("DataStoreService")
3 
4-- Create a data store called "PlaceData"
5local placeData = datastore:GetDataStore("PlaceData")
6 
7-- Get the current place id from the game.
8local placeVersion = game.PlaceVersion

Next, send a get request to the data store to retrieve what will represent the last place version saved.

01local datastore = game:GetService("DataStoreService")
02local placeData = datastore:GetDataStore("PlaceData")
03local placeVersion = game.PlaceVersion
04 
05-- Get the key "LastPlaceVersion" from data store "PlaceData"
06local lastPlaceVersion = placeData:GetAsync("LastPlaceVersion")
07 
08-- If it's nil (which indicates it's the first time it's saving the place version), then set it with the place version that we got from game.PlaceVersion
09if lastPlaceVersion == nil then
10    placeData:SetAsync("LastPlaceVersion", placeVersion)
11    lastPlaceVersion = placeVersion -- Also update the variable in case you need to use it again somewhere else in the script.
12end

Now last but not least, compare "lastPlaceVersion" with "placeVersion" and see if they're not equal to each other. If they're not, that means you updated your place since a user last played, and you can then set all your OrderedDataStore keys to nil. Here would be the final implementation:

01local datastore = game:GetService("DataStoreService")
02local placeData = datastore:GetDataStore("PlaceData")
03local placeVersion = game.PlaceVersion
04 
05local lastPlaceVersion do
06    lastPlaceVersion = placeData:GetAsync("LastPlaceVersion")
07    if lastPlaceVersion == nil then
08        placeData:SetAsync("LastPlaceVersion", placeVersion)
09        lastPlaceVersion = placeVersion
10    end
11end
12 
13-- Compare the place version with the last place version
14if placeVersion ~= lastPlaceVersion then
15 
View all 28 lines...

That's how I'd personally go about doing this, but if you're confused about anything or if you have any questions, just let me know. I've also tested this in studio before posting the answer to ensure it works.

Ad
Log in to vote
-1
Answered by
Uglypoe 557 Donator Moderation Voter
8 years ago
Edited 8 years ago

As far as I know, the easiest way to do it would just be to change the variable for the data store.

Say your game is V1.0. Set this as the Data Store where all data saves:

1ods = game:GetService("DataStoreService"):GetOrderedDataStore("LeaderboardV1.0")

Then maybe you want to reset the leaderboard. Do something like this:

1ods = game:GetService("DataStoreService"):GetOrderedDataStore("LeaderboardV1.1")

It's just an example, the variable can be anything. Just change the variable and set all save functions towards the new data store.

EDIT: If you want it to erase after every update, the DataModel has a handy "PlaceVersion" value. Just set up the data store like this:

1ods = game:GetService("DataStoreService"):GetOrderedDataStore("LeaderboardV"..game.PlaceVersion)

This will "erase" the data store after every update to the game.

Answer this question