Datastore not saving

I found a datastore script from the forum and edited it but i couldnt get it to work.
When i leave the game its supposed to save the data but it doesnt. it just prints

Data has not been saved

game.Players.PlayerRemoving:Connect(function(player) -- When a player leaves
	local success, err  = pcall(function()
		saveData(player) -- Save the data
	end)

	if success then
		print("Data has been saved")
	else
		print("Data has not been saved!")
	end
end)

And this is the function

local function saveData(player) -- The functions that saves data

	local tableToSave = {
		
            player.Stats.Tokens.Value;
            player.Stats.TotalTokens.Value;
	}

	local success, err = pcall(function()
		dataStore:UpdateAsync(player.UserId, tableToSave) -- Save the data with the player UserId, and the table we wanna save
	end)

	if success then -- If the data has been saved
		print("Data has been saved!")
	else -- Else if the save failed
		print("Data hasn't been saved!")
		warn(err)		
	end
end

This isnt the all of the code. i just posted the part where i think thats not working.

:UpdateAsync second argument is a function which returns the value to be saved

local function saveData(player) -- The functions that saves data

	local tableToSave = {
		
            player.Stats.Tokens.Value;
            player.Stats.TotalTokens.Value;
	}

	local success, err = pcall(function()
		dataStore:UpdateAsync(player.UserId, function() return tableToSave end)
	end)

	if success then -- If the data has been saved
		print("Data has been saved!")
	else -- Else if the save failed
		print("Data hasn't been saved!")
		warn(err)		
	end
end

Try removing the pcall for testing, then you can see the error and stack. Once you’re sure it is working, then you should put the pcall back to ignore roblox errors.

Solved the issue by using SetAsync() instead of UpdateAsync()

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.