The expected result of the SaveData() function in the code below is to print "Saved data for Player1." However, it NEVER prints this. I'm having trouble because this also occurs when I replace UpdateAsync() with SetAsync(). No error is returned either.
Here's the whole script:
local DataManager = {}
local DataStoreService = game:GetService("DataStoreService")
local PlayerData = DataStoreService:GetDataStore("PlayerData")
local SessionData = {} -- Current game data
local StartingData = -- Data for players who have never played.
{
Cash = 0,
Experience = 0,
Level = 0,
TotalDamage = 0,
EquippedWeapon = "Stick",
Weapons = {"Stick"}
}
local function SaveData(Player)
local PlayerId = Player.UserId
local Data = SessionData[PlayerId]
print(Player.Name .. " left. Saving data.")
if Data then
local success, err = pcall(function()
PlayerData:UpdateAsync(PlayerId, function(old) return Data end)
end)
if not success then print(err) error("Cannot save data for " .. Player.Name .. "!") end
end
print("Saved data for " .. Player.Name .. ".")
end
game.Players.PlayerRemoving:Connect(function(Player)
SaveData(Player)
end)
return DataManager

9 comments
I'm guessing this is not the only code, you also missed the first and last line in the codeblock. To begin if there are no errors it's either working or not running, try printing something on line 1 to see if it is. If it's not running it's either disabled or misplaced.
I've posted the whole script. I can't seem to find the problem, as it runs fine otherwise.
Why is it so spaced out >.<
How I code man, I don't like big clumps of text. Same reason they alternate colors on charts
Can you at least cut your code into sections and tell us what each of those sections do? There's a lot of things to read and saving some time means I could also be helping someone else.
This depends are you testing this in studio or on a real server?
Will cut code and using APS, so it shouldn't matter @Syntax
Cut the code. I left in the code that I have the problem with
Why use UpdateAsync when you're using it like you'd use SetAsync? SetAsync overrides the current data, which is what you're already doing