Checking if DataStore has data

In my game, Im currently making a File Selection GUI, but i want to check if a DataStore has any data in it to prevent filling up the queue.
Is there any way to do this?
Do you have any help or tips?

4 Likes

DataStore:GetAsync(key) will return nil if no data is saved. In the case of multiple files. You should have a additional key which contains data such as level, progress, and whether data is saved, and the key.

7 Likes

GetAsync is the only retrieval method available that can look into a DataStore by the key you specify. If you have multiple save files, you can try scoping your data. You may incur a hefty cost though.

-- A pretty ugly example

local DataStoreService = game:GetService("DataStoreService")
local PlayerDataStorage = DataStoreService:GetDataStore(UserId)
local Saves = PlayerDataStorage:GetAsync("Saves") --> {"Save1", "Save2", ...}
local ChosenSave = PlayerDataStorage:GetAsync(Saves[1])

There’s of course a better way to do this which I’d probably do as well (it involves DataStore scoping), but this is what I’ve got for now.

We use the player’s UserId to help us make it easier to do things. We could also, alternatively, use a DataStore named “PlayerData” with the player’s UserId as the scope, for organisation’s sake.

The code uses two Get requests initially; one gets save files which is cached for the rest of the session, then one to get the data saved to a key. We use the first request to find out what saves a player has, then the second to get data based on the contents of that table.

1 Like