Saving tables for UpdateAsync() properly

I’m attempting to use UpdateAsync() to save a table, but it doesn’t seem to work out as intended. I’m currently using logs to test an attempt of it working, which saves the user’s message and who wrote it, but I’m having an issue retrieving it from the datastore. I tried doing furthermore research that could fix the issue that I’m experiencing, but doesn’t seem to refer to what I’m looking for. What’s the concurrent issue here?

-- script:
--[ Variables
local rep = game:GetService("ReplicatedStorage")
local dds = game:GetService("DataStoreService")
local lds = dds:GetDataStore("lds")
--[ Setup
game.Players.PlayerAdded:Connect(function(plr)
	plr.Chatted:Connect(function(msg) -- event starts
		plr = tostring(plr)
		local success, failed = pcall(function()
			lds:UpdateAsync("lds", function(tbl) -- updates with a table
				tbl = {
					Message = msg, -- data.Message (if I'm right)
					PlayerFrom = plr -- data.PlayerFrom (if I'm right)
				}
				return tbl
			end)
		end)
		if success then
			print("The datastore has been updated")
		elseif failed then
			warn(failed)
		end
		rep.LogFired:FireAllClients(msg, plr)
	end)
end)
local data
local success, failed = pcall(function()
	data = lds:GetAsync("lds")
end)
if success then
	for _, i in pairs(data) do
		print(data[i], data[i].Message, data[i].PlayerFrom)
		rep.LogFired:FireAllClients(data[i].Message, data[i].PlayerFrom) -- getting their data
	end
elseif failed then
	warn(failed)
end

-- localscript that responds to the remote
--[ Variables
local cp = game:GetService("ContentProvider")
local rep = game:GetService("ReplicatedStorage")
local frame = script.Parent
--[ Setup
for _, i in pairs(frame:GetChildren()) do
	cp:PreloadAsync({i})
end
rep.LogFired.OnClientEvent:Connect(function(msg, plr)
	local clone = rep.dCard:Clone()
	clone.Description.Text = msg.." /by "..tostring(plr)
	clone.Parent = frame.Logs
end)

Here’s a little snippet of how you could handle the saving of chat logs.

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local ChatLogsStore = DataStoreService:GetDataStore("ChatLogs")

Players.PlayerAdded:Connect(function(player)

    player.Chatted:Connect(function(message)
        local success, err = pcall(function()
            ChatLogsStore:UpdateAsync("ChatLogs", function(chatLogs)
               local messageData = {
                  Message = message, — message they sent
                  Sender = player.Name — the player’s name
                }

                if not chatLogs then
                     chatLogs = {} — if the chat logs hasn’t been created, then create it!
                end

                chatLogs["ChatLog ".. #chatLogs + 1] = messageData — update the chat logs table with the new chat log
                return chatLogs — save the chat logs table
        end)
    end)
end)

        if not succcess then
              warn("There was an error whilst saving to chat logs!")
              warn("Error:", err)
        end

end)
1 Like

The second argument of the UpdateAsync, which is a function, is the old data of the key. When doing UpdateAsync, it is expecting that a data will be returned. So, you can just create a table and return that table.

Another note is that you can’t save Instances. What you are doing is saving a Player object.