local datastore = game:GetService(“DataStoreService”):GetDataStore(“name”)
Please note that if a data store has not been created yet, it will automatically create a new one.
The following code will output nil, because the server couldn’t find any value linking to the key; it is important to show the server exactly what we are trying to output, so that the server will know what needs to be displayed. local datastore = game:GetService(“DataStoreService”):GetDataStore(“name”) game. Players. PlayerAdded:connect(function(player) local key = “user_” . . player. userId datastore:GetAsync(key) end)
If the previous set of information is important, consider using UpdateAsync, which will be taught below. The following code shows you how to implement both, the “:GetAsync()”, and the “:SetAsync()”, methods. local datastore = game:GetService(“DataStoreService”):GetDataStore(“name”) game. Players. PlayerAdded:connect(function(player) local key = “user_” . . player. userId datastore:SetAsync(key, 90) – sets the key to the value, 90 local data_stored = datastore:GetAsync(key) – is able to detect the value change print(data_stored) – prints the output end) Note: This will not work, unless you have the API access enabled. To do this, read the first instruction of this guide.
local datastore = game:GetService(“DataStoreService”):GetDataStore(“name”) game. Players. PlayerAdded:connect(function(player) local key = “user_” . . player. userId datastore:UpdateAsync(key, function(old) – do stuff end) end) In this case, we called the old value “old”. Inside this function, we will need to make a variable that will account for our updated score, and then return that so it can display our new score. local datastore = game:GetService(“DataStoreService”):GetDataStore(“name”) game. Players. PlayerAdded:connect(function(player) local key = “user_” . . player. userId datastore:UpdateAsync(key, function(old) local new = old or 0 – could be nil new = new + 1 – add 1 to the old value return new – returns it with the new value end) end) Note that the server will return nil if the key does not exist or is not assigned correctly. If the function does not exist, the update will be cancelled.
local datastore = game:GetService(“DataStoreService”):GetDataStore(“name”) game. Players. PlayerAdded:connect(function(player) local key = “user_” . . player. userId end) This will automatically create a key that is unique to that player only, for every player will have one unique ID. The “user_” will not matter.
Start with a function to help the server understand what you are intending to do. local datastore = game:GetService(“DataStoreService”):GetDataStore(“name”) game. Players. PlayerAdded:connect(function(player) local key = “user_” . . player. userId datastore:UpdateAsync(key, function(old) local newValue = old or 0 – could be nil newValue = newValue + 50 return newValue end) end) In this function, we set up another function, old. “old” was our previously saved data. In this scenario, every time a player entered the server, the server would locate its key, which is their userId, and it would update the data by 50 points, returning and displaying that new value.