New scripter working with datastores

Hi all of the scripters out there!

I am a new scripter to Roblox and I am currently working with datastores (with some help from the Developer Hub)

This is my current datastore script:

local DataStoreService = game:GetService("DataStoreService")

local experienceStore = DataStoreService:GetDataStore("PlayerExperience")
	
	local success, errorMessage = pcall(function()
	return experienceStore:SetAsync("User_1234", 50)
	end)
		
		if not success then
			print(errorMessage)
		end

But on return experienceStore:SetAsync("User_1234", 50) I am quite unsure what the User_1234 is supposed to do. Is it an already existing variable in Studio or am I supposed to change it to something else? If I don’t change it is it just not gonna save things to the datastore? I’m not rather sure yet what game to make and if I do I am gonna work with a friend, so I am creating some hand-made scripts before-hand. Also, the “50” beside User_1234, is that for example score, cash, points or whatever shown on the leaderboard? I’m really confused so I’d appreciate some help.

User_1234 stands for the key in this case, datastoring works with keys and values, just like dictionaries, it looks something like this:

local dictionary = {
   somekey = "somevalue",
   anotherkey = "anothervalue"
}

That is a quite random example, but more to the specific code that you got, the datastore looks something like this after saving:

datastore = {
   User_1234 = 50
}

User_1234 is the key here, to which the value is saved, using that key, and that key only, you can retrieve the value of it.

2 Likes

values in setAsync can be changed
but values in GetAsync must be the same otherwise it does weird things
try

local DataService = game:GetService("DataStoreService"):GetDataStore('PlayerExperience')
DataService :SetAsync('qin2007 is the best',true)
print(DataService:GetAsync('Qin2007 is not the best'),'this is not true')
print(DataService:GetAsync('qin2007 is the best'),'this is true')
1 Like

I dont really like the dev hub. It’s only good to see the api’s in my opinion. Look at this tutorial instead, it’s how I learned how

DataStores may seem difficult at first but I’m going to try and oversimplify it and make them more accessible.

When you first get the DataStore, you use this method

DataStoreService:GetDataStore(data_store: string, scope: string)

It returns a DataStore which consists of two main methods.

These two methods being GetAsync(key: string) and SetAsync(key: string, value: any)

In programming we use the key to represent a unique string to get a value.

Think of it as a physical key, you have unique keys, each one of them used to unlock a different players data.

You have a key, with a door with the data behind it. That’s the GetAsync, method.

Then have ``, where after getting the data you can write it then put it back behind the door with the key.

Here’s a very juvenile emoji diagram:

GetAsync: :page_facing_up::door::key: (taking the paper to write on it)
SetAsync: :memo::key::door: (writing the paper and putting it back behind the door with the key)

local cash = DataStore:GetAsync("Player_" .. player.UserId) or 50 --> Cash: 50

data += 50 --> Cash: 100

DataStore:SetAsync("Player_" .. player.UserId)

I don’t find it thaaat difficult, I’m pretty good when it comes to debugging etc.

So am I in part of the script supposed to use GetAsync and then SetAsync on another? If so, it makes sense.

@yungpharma (tagging you as it seems like the reply didn’t go through)

when the player joins, you will use getasync and you can cache their data

example:

local cache = {} -- store it in a table

local DataStore = ... -- datastore

local function playerAdded(player)
	local data = DataStore:GetAsync("Player_" .. player.UserId)

	cache[player.UserId] = data
	-- or ...
	local leaderstats = ... -- folder

	local cash = ... -- intvalue
	cash.Value = data

	-- attributes method
	player:SetAttribute("Cash", data)
end

you’re gonna use setasync when the player leaves to save their data

setasync sets (writes) the data that is in the “cloud”

local function playerRemoving(player)
	-- if caching
	DataStore:SetAsync("Player_" .. player.UserId, cache[player.UserId]


	-- more annoying way
	local leaderstats = player:WaitForChild("leaderstats")
	local Cash = leaderstats:WaitForChild("Cash")
	DataStore:SetAsync("Player_" .. player.UserId, Cash.Value)
	
	-- the new method, using attributes
	DataStore:SetAsync("Player_" .. player.UserId, player:GetAttribute("Cash"))
end

Ok so first of all the User_1234 does not have to stay as a user or userId it can be anything that you want it to be, it is called a key, the part after the key 50 is the data that is stored in the specific key. This data can be changed to anything such as a Dictionary, Table, String, Bool, Int… You can use Set Async to set the value of a key in this case User_1234 the value/data in the next Parameter which in this case would be 50. You can also use Update Async but that’s a whole other thing. Another useful thing is receiving the data Get Async To retrieve the data and use it in whatever case you need it. However there are limits to the datastore, you can see them here. There are so many possibilities with datastore you just have to know-how.

I am a datastore developer

Thank you! This cleared my brain when it came to this. I will be updating my scripts and adding more scripts.

I will obviously come here to Scripting Support when I run into any more problems.

1 Like