Hello fellow developers! Datastore is one of the most important services in a Roblox game. I’m trying to use UpdateAsync() instead of SetAsync().
This is a function which saves player’s data when they leave the game.
local function saveData(player,tableToSave)
print("Function Executed")
local success, err = pcall(function()
dataStore:UpdateAsync(player.UserId, function(compareTable)
print(compareTable)
if compareTable == nil then --NewPlayer
local NewTableToSave = tableToSave
print(NewTableToSave)
return NewTableToSave
elseif compareTable then
print(compareTable)
print(tableToSave)
return tableToSave
end
end)
end)
print("Code Executed")
if success then
print(player,"'s Data has been saved!")
else
print(player,"'s Data hasn't been saved!")
warn(err)
end
end
This is the line which calls the function:
game.Players.PlayerRemoving:Connect(function(player)
print(player)
local success, err = pcall(function()
print(player,"SuddenContinued")
saveData(player,{
["Leaderstats"] = {["UCoins"] = LeaderstatsFol.UCoins.Value, ["Diamonds"] = LeaderstatsFol.Diamonds.Value},
["PlayerOverallStats"] = {
["Tutorial"] = SaveDataFol:FindFirstChild("TutorialDone").Value
},
["Inventory"] = {
["Hotbar"] = {
[1] = {["WeaponName"] = HotbarFol:FindFirstChild("Hotbar1"):FindFirstChild("WeaponName").Value, ["Type"] = HotbarFol:FindFirstChild("Hotbar1"):FindFirstChild("Type").Value},
[2] = {["WeaponName"] = HotbarFol:FindFirstChild("Hotbar2"):FindFirstChild("WeaponName").Value, ["Type"] = HotbarFol:FindFirstChild("Hotbar2"):FindFirstChild("Type").Value},
},
["InventoryBackpack"] = {
["Melee"] = GetInvWeapon("MeleeWeapon"),
},
},
}) -- Save the data
end)
print(player,"Continued2")
if success then -- If the data has been saved
print(player,"'s Data has been saved!")
else -- Else if the save failed
print(player,"'s Data hasn't been saved!")
warn(err)
end
print(player,"Continued3")
end)
It prints player, the “SuddenContinued” and “Function Executed”)
But it doesn’t print compareTable, “Continue3” and the other else.
Anyone know why?
Did I do anything wrong while coding this?
Any comments are appreciated!
(Edit: I just make the layout better so it is easier for you to read.)