Continuing the discussion from Leaderstats and datastore :
local DataStoreService = game:GetService("DataStoreService")
local playerData = DataStoreService:GetDataStore(“playerData”)
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new(“Folder”)
leaderstats.Name = “leaderstats”
leaderstats.Parent = player
local gold = Instance.new("IntValue")
gold.Name = "Gold"
gold.Parent = leaderstats
local data
local success, errormessage = pcall(function()
data = playerData:GetAsync(player.UserId.."-gold")
end)
if success then
gold.Value = data
else
print("Error while getting your data")
warn(errormessage)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local success, errormessage = pcall(function()
playerData:SetAsync(player.UserId.."-gold", player.leaderstats.Gold.Value)
end)
if success then
print("Data successfully saved!")
else
print("There was an error while saving the data")
warn(errormessage)
end
end)
1 Like
Everything looks good in your code although I would like to give some suggestions:
Please don’t use SetAsync though, as much as I know UpdateAsync is far better to use.
You should implement a BindToClose event saving all the player’s data too, this thread might help you.
That’s all I really have to say, there are other things like you could try resaving if there was failure etc. But I am not really sure about it.
colbert2677
(ImagineerColbert)
September 27, 2020, 7:26pm
#3
Please do not repost threads or content. If you want to continue discussion, you can post that you’ve made an update and are looking for further feedback or recategorise the thread properly by clicking the pencil beside the title.
2 Likes