How would you use ROBLOX's datastorage API?

Hello!

I have a few questions about datastorage on ROBLOX. After reading about the API on the wiki, I still don’t have an idea of how I’d go about using it efficiently.

  • How do you correctly set and get data from a datastorage? (Player level, player items, last save point, etc)
  • Is any preparation needed? I’m familiar with database hosts like PHPMyAdmin, and I was wondering if it’s similair to that in any sort of way in terms of normalizing and accessing the datastorage from the server.
  • Are there any things I should look out for or absolutely NOT do when using the datastorage API?

Thanks in advance.

3 Likes

You assign each player a key, which could be anything, usually use their userId

players.CharacterAdded:Connect(function(p)
local key = "keyp-" .. p.UserId
end)

You’d then use Get and SetAsync to access and save their data appropriately.

local DDS = game:GetService("DataStoreService")
local MainData = DDS:GetDataStore("DataStoreName")
pcall(function()
Data = MainData:GetAsync(key) --Returns data saved
MainData:SetAsync(key, Data) -- Saves data to the Datastore
end)
3 Likes

Setting Up and Loading Data

First you’ll want to setup a template of the data you need to save, for example:

function GetDataTemplate()
	return{
		Exp = 0;
		Level = 0;
		Cash = 0;
		Items = {};
	}
end

When a player enters the game, you can use GetAsync(key) to check for existing data. I would strongly recommended using the player’s UserId for the key, as there is no chance of this being altered (unlike the player’s username). Make sure to wrap your requests in pcalls because they can sometimes fail:

local success, pdata = pcall(function() return dataStore:GetAsync(key) end)

Once you’ve done this, you’ll want to make three checks:

  1. Were you able to access the dataStore? (i.e. does success == true?) If not, then set the player’s data to false and take the appropriate action (e.g. warn the user or teleport them back into the game)
  2. If successful, does the player’s data exist? If not, they’re a new player, and you need to setup their data (i.e. using GetDataTemplate())
  3. Else the player has existing data; use this. You may want to compare this data against GetDataTemplate() if you plan on adding more data to your template after your game has been released.


Saving Data

There are various stages where you might want to save a player’s data. For example:

  • When the player leaves the game
  • Every x seconds
  • When the player purchases an important item

There are four methods of writing data:

  1. UpdateAsync
  2. SetAsync
  3. IncrementAsync
  4. RemoveAsync

In your situation, I would strongly recommend using UpdateAsync() as this enables you to compare the player’s current data with the data about to be overwritten, making any adjustments as needed. For example, you could define a variable called ‘DataID’ and increase this value by 1 every time the player’s data is saved. If both IDs do not much (e.g. because the player’s data failed to load correctly), then you can cancel the save and prevent the player’s data being overwritten.

You must also consider limitations when using functions like UpdateAsync(). For example, you must leave a 6 second interval before updating the same key again. You can find out more on Datastore limitations and errors here: Documentation - Roblox Creator Hub



There are range of useful resources for creating datastores. For example:
https://developer.roblox.com/articles/Data-store

https://devforum.roblox.com/t/good-practices-for-datastorage/81985/5

62 Likes

@Wingz_P, @ForeverHD

Thank you for your explanation!
This gave me a much better insight on how it works and how it should be used.

3 Likes