I’m kind of new to data stores and UpdateAsync, so if anyone knows the problem, please tell me!
local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")-- :GetDataStore means you're getting a data store that has been saved in the game.
-----------------Loading Data------------------
game.Players.PlayerAdded:Connect(function(player)
--Data
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local Cash = Instance.new("IntValue")
Cash.Name = "Cash"
Cash.Parent = leaderstats
local Rebirths = Instance.new("IntValue")
Rebirths.Name = "Rebirths"
Rebirths.Parent = leaderstats
--Codes
local codeStats = Instance.new("Folder")
codeStats.Name = "CodeStats"
codeStats.Parent = player
local codes = require(game:GetService("ReplicatedStorage").Modules.Codes)
for i, Code in ipairs(codes) do
local bool = Instance.new("BoolValue")
bool.Name = Code
bool.Parent = codeStats
end
local playerUserId = "Player_"..player.UserId
--Load Data
local data
local success, errormessage = pcall(function()
data = myDataStore:GetAsync(playerUserId)
end)
if success then
if data then
Cash.Value = data.Cash
Rebirths.Value = data.Rebirths
--Codes
local codes = require(game:GetService("ReplicatedStorage").Modules.Codes)
for i, code in ipairs(player.CodeStats:GetChildren()) do
for dataValue, v in pairs(data) do
if dataValue == code.Name then
code.Value = dataValue
end
end
end
end
end
end)
-----------------------Saving Data-------------------------
game.Players.PlayerRemoving:Connect(function(player)
local playerUserId = "Player_"..player.UserId
local data = {
Cash = player.leaderstats.Cash.Value;
Rebirths = player.leaderstats.Rebirths.Value;
}
for i, v in ipairs(player.CodeStats:GetChildren()) do
data[v.Name] = v.Value
end
local success, errormessage = pcall(function()
myDataStore:UpdateAsync(playerUserId, function(oldData)--This is the part I changed from GetAsync to UpdateAsync.
return data
end)
end)
end)