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?
Clearing an OrderedDataStore based on version number?
2 answers
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:
-- Get the data store service
local datastore = game:GetService("DataStoreService")
-- Create a data store called "PlaceData"
local placeData = datastore:GetDataStore("PlaceData")
-- Get the current place id from the game.
local placeVersion = game.PlaceVersion
Next, send a get request to the data store to retrieve what will represent the last place version saved.
local datastore = game:GetService("DataStoreService")
local placeData = datastore:GetDataStore("PlaceData")
local placeVersion = game.PlaceVersion
-- Get the key "LastPlaceVersion" from data store "PlaceData"
local lastPlaceVersion = placeData:GetAsync("LastPlaceVersion")
-- 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
if lastPlaceVersion == nil then
placeData:SetAsync("LastPlaceVersion", placeVersion)
lastPlaceVersion = placeVersion -- Also update the variable in case you need to use it again somewhere else in the script.
end
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:
local datastore = game:GetService("DataStoreService")
local placeData = datastore:GetDataStore("PlaceData")
local placeVersion = game.PlaceVersion
local lastPlaceVersion do
lastPlaceVersion = placeData:GetAsync("LastPlaceVersion")
if lastPlaceVersion == nil then
placeData:SetAsync("LastPlaceVersion", placeVersion)
lastPlaceVersion = placeVersion
end
end
-- Compare the place version with the last place version
if placeVersion ~= lastPlaceVersion then
-- Print the last version update
print("Game was updated from version: " .. lastPlaceVersion .. " to version: " .. placeVersion)
-- This would be where you would change all of your OrderedDataStore keys to nil, to remove them when you updated your place.
print("Your ordered data store code here")
-- Don't forget to update the value of the last place version when
-- it's different!
placeData:UpdateAsync("LastPlaceVersion", function()
return placeVersion
end)
end
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.
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:
ods = game:GetService("DataStoreService"):GetOrderedDataStore("LeaderboardV1.0")
Then maybe you want to reset the leaderboard. Do something like this:
ods = 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:
ods = game:GetService("DataStoreService"):GetOrderedDataStore("LeaderboardV"..game.PlaceVersion)
This will "erase" the data store after every update to the game.
This discussion is preserved as it was. New answers, comments, and votes are closed.
