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, SetAsyncis 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 use UpdateAsync 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)
- Take care of leaving players, using the PlayerRemoving event (RBXScriptSignal)
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
5 comments
returns GetAsync is not a member of DataStoreService
Oh, I forgot to actually create or reference a data store using
GetDataStoreI'm still having a lot of issues, no output errors
what kinds of issues?
I had a few issues including the fact that both events were
PlayerAddedinstead of one beingPlayerRemoving. Also, I tried to save an instance instead of it's value in the UpdateAsync function. Fixed.