I haven't attempted to use Data Store
yet, and I'd like to get familiar with it. I've looked at wikis and other questions and don't really understand it. Here's my script to attempt using data stores:
local DataStore = game:GetService("DataStoreService") game.Players.PlayerAdded:connect(function(player) local key = "user_" .. player.userId DataStore:UpdateAsync(key, function(oldKills) local Kills = oldKills or 0 Kills = Kills + 50 return Kills end) end) game.Players.PlayerRemoving:connect(function(player) local key = "user_".. player.userId local KOs = (tostring(player.leaderstats.Kills)) DataStore:SetAsync(key,KOs) end)
As I said I don't at all understand this, and don't know how I got to this point. What are the arguments with the UpdateAsync, SetAsync, and GetAsync functions?
Your code is pretty close to being accurate except the fact that you might want to save the value when the player leaves rather than joins the server, using UpdateAsync
. Also, SetAsync
is pretty much similar to UpdateAsync
in the sense that they both change the DataStore.
However, if you're interested in getting previously saved values in your querry, you would want to useUpdateAsync
because it will get the already existing value of the key, run the transformFunction
(aka second argument), and then updating the key. SetAsync
, is mostly used for constant values, and overrides the value of the key with the newly set value.
GetAsync
just simply means fetching the value associated with the provided key
OnClose
ensures that ROBLOX's server cannot shutdown until the provided Callback
(argument) has finished. As of now, the Callback can only yield for a maximum of 30 seconds. I would recommend using 5 seconds in this context.
In other words, UpdateAsync would be best in this context.
local DataStore = game:GetService("DataStoreService") local SavedData = DataStore:GetDataStore("PlayerKills") local Players = game:GetService("Players")
Players.PlayerAdded:connect(function (player) local key = ('user_'..player.userId) local saved = SavedData:GetAsync(key) or 0 local leaderstats = Instance.new('Folder') leaderstats.Parent = player leaderstats.Name = 'leaderstats' local kills = Instance.new('NumberValue') kills.Name = 'Kills' kills.Parent = leaderstats kills.Value = saved end)
Players.PlayerRemoving:connect(function (player) local key = ('user_'..player.userId) local stats = player:WaitForChild('leaderstats') local kills = stats:WaitForChild('Kills') SavedData:UpdateAsync(key, function(oldKills) local newKills = oldKills or kills.Value print('new',newKills) return newKills end) end)
game.OnClose = function() wait(5) end
Altogether..
local DataStore = game:GetService('DataStoreService') local SavedData = DataStore:GetDataStore('PlayerKills') local Players = game:GetService('Players') Players.PlayerAdded:connect(function (player) local key = ('user_'..player.userId) local saved = SavedData:GetAsync(key) or 0 print('old',saved) local leaderstats = Instance.new('Folder') leaderstats.Parent = player leaderstats.Name = 'leaderstats' local kills = Instance.new('NumberValue') kills.Name = 'Kills' kills.Parent = leaderstats kills.Value = saved end) Players.PlayerRemoving:connect(function (player) local key = ('user_'..player.userId) print(key) local stats = player:WaitForChild('leaderstats') local kills = stats:WaitForChild('Kills') SavedData:UpdateAsync(key, function(oldKills) local newKills = oldKills or kills.Value print('new',newKills) return newKills end) end) game.OnClose = function() wait(5) end
more on data stores here