CODE
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local CashDataStore = DataStoreService:GetDataStore("CashDataStore")
Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder", player) -- I don't think this would affect preformance heavily.
leaderstats.Name = "leaderstats"
local cash = Instance.new("IntValue", leaderstats)
cash.Name = "Cash"
local data = nil
local success, err = pcall(function()
data = CashDataStore:GetAsync(player.UserId.."-cash")
end)
if success then
if data then
cash.Value = data
else
cash.Value = 100 -- start out with 100 cash
end
else
warn("There was an error whilst getting " .. player.Name .. "'s data!")
warn("Error code: ".. err)
end
end)
Players.PlayerRemoving:Connect(function(player)
local cash = player.leaderstats.Cash
local success, err = pcall(function() -- how do I use properly?
CashDataStore:UpdateAsync(player.UserId.."-cash", function(oldData)
if oldData then
return oldData
else
return cash.Value
end
end)
end)
if success then
print(player.Name .. "'s data have been saved successfully!")
else
warn("There was an error whilst saving " .. player.Name .. "'s data!")
warn("Error code: ".. err)
end
end)
game:BindToClose(function() -- how would I use this correctly as well?
for _, player in pairs(Players:GetPlayers()) do
CashDataStore:UpdateAsync(player.UserId.."-cash", function(oldData)
if oldData then
return oldData
else
return player.leaderstats.Cash.Value
end
end)
end
end)
I got the warning,
WARNING
DataStore request was added to the queue. If the request queue fills,
further requests will be dropped. Try sending fewer requests.Key = 277320496-cash
It might be because of my misuse of the :BindToClose() function.