Why is this script not saving data?

I’ve been having this issue for a while, and basically I have a script that is supposed to save data using update async, but for some reason its not. There are no errors in the output. Nothing.

Load data function:

local function loadPlayerData(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	local currency1 = Instance.new("IntValue")
	currency1.Name = "Coins"
	currency1.Parent = leaderstats
	local currency2 = Instance.new("IntValue")
	currency2.Name = "Gems"
	currency2.Parent = leaderstats
	local data = nil
	local success, err = pcall(function()
		data = dataStore:GetAsync(player.UserId)
	end)
	if success then
		print("Successfully loaded player data for "..player.Name)
			
		if data then
			loadedPlayerData[player] = data
			currency1.Value = data.Coins
			currency2.Value = data.Gems
		else
			loadedPlayerData[player] = {DataId = 0, Coins = defaultCoins, Gems = 0}
			currency1.Value = defaultCoins
			currency2.Value = 0
		end
	else
		warn("Data Store: "..err)
	end
end

Save data function:

	local playersData = loadedPlayerData[player]
	if playersData then
		local success, err = pcall(function()
		    dataStore:UpdateAsync(player.UserId, function(oldValue)
                print(1)
			    local previousData = oldValue or {DataId = 0}
                print(2)
			    if playersData.DataId == previousData.DataId then
				    print(3)
				    playersData.DataId = playersData.DataId + 1
				    print(4)
				    return playersData
			    else
			    	print(5)
			    	warn("Couldn't save data for "..player.Name)
				    print(6)
			    	return nil -- rip
			    end
		    end)
		end)
		if success then
			print("Successfully saved player data for "..player.Name)
		else
			warn("Data Store: "..err)
		end
		loadedPlayerData[player] = nil
    end
end

The save data function is inside a player removing function. It is supposed to print 1, 2, 3, 4. But it doesn’t print anything at all. Can someone please tell me why this isn’t doing anything?

First parameter of :GetAsync / :UpdateAsync is a string, you’re using a number. Try to do something like "player_" .. player.UserId instead. (like a string-key).

Besides, saving on leaving could sometimes not work because the server won’t have enough time to save the data for the player. I recommend using ProfileService or DataStore2 to save data more efficiently.

1 Like

I am confused with the lines saying loadedPlayerData[player] = player. Are you calling the function or what?

1 Like

loadedPlayerData is actually a table. local loadedPlayerData = {}. Then we are inserting the player’s playerdata into that table by doing loadedPlayerData[player] = data.

To remove it from the table, all you have to do is… loadedPlayerData[player] = nil.

Try and add this to your script:

game:BindToClose(function()
    if game:GetService(“RunService”):IsStudio() then
        wait(4)
    else
        wait(30)
    end
end)

It should give your code time to run.

I tried replacing player.UserId with tostring(player.UserId). It still didn’t fix the problem. Can someone please help me get this problem fixed?