I’m currently making a datastore for my game, and I was recommended to use UpdateAsync() and also to save a table to a datastore instead of having like five of them. The issue is, when I try saving my table of data, it doesn’t seem to work…
There are no errors, but here is the script.
game.Players.PlayerAdded:Connect(function(player)
local playerDataTable = {
["studData"] = 0,
["totalBricks"] = 0,
["savedKillSound"] = shopModule.shopAssets.killSounds.default,
["playerItemData"] = {
Weapons = {},
KillSounds = {},
},
["shopItemData"] = {},
}
local killSound = Instance.new("Sound", player)
killSound.Name = "killSound"
killSound.Volume = 2
killSound.SoundId = game:GetService("ReplicatedStorage"):WaitForChild("KillSounds"):WaitForChild("default").SoundId
local items = nil
local leaderstats = Instance.new("Folder", player)
leaderstats.Name = "leaderstats"
local Studs = Instance.new("NumberValue", leaderstats) -- The Currency
Studs.Name = "Studs"
Studs.Value = 0
local Bricks = Instance.new("IntValue", leaderstats) -- The Bricks broken between matches
Bricks.Name = "Bricks"
Bricks.Value = 0
local totalBricks = Instance.new("IntValue", leaderstats) -- All time bricks broken
totalBricks.Name = "T. Bricks"
totalBricks.Value = 0
local datatable
local success, errorMessage = pcall(function()
datatable = tableDataStore:GetAsync(player.UserId)
end)
if success then
playerDataTable = datatable
elseif errorMessage then
warn(player.Name.."'s data cannot be found!..")
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local leaderstats = player.leaderstats
local dataToSave = {
["studData"] = player.leaderstats.Studs.Value,
["totalBricks"] = player.leaderstats["T. Bricks"].Value,
["savedKillSound"] = player.killSound.SoundId,
["playerItemData"] = {
Weapons = {},
KillSounds = {},
},
["shopItemData"] = {},
}
print(dataToSave)
tableDataStore:UpdateAsync(player.UserId, function(passData)
return dataToSave
end)
local playerItemTable
for i, v in pairs(shopModule.listOfPlayerItemTables) do
if v.ownerTag == player.Name then
playerItemTable = v
end
end
end)