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.
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')
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.