UpdateAsync or GetAsync Isn't Working

Hey, so I’m not sure what’s wrong, but I may be using UpdateAsync incorrectly. Every time I use

members = Work:GetAsync(code)
print(#member.Work)

0 is printed even though I just added something to the table

Script

local Work = DataStore:GetDataStore("Classwork")

local function createCode(Player, code)
	members = Classes:GetAsync(code)
	if not members then
		Work:SetAsync(code, {Work = {}})--Creates a key and a table called Work
	end
end

--THIS IS THE PART I'M HAVING PROBLEMS WITH
local function addWorkToClass(player,code,subject)--receives the player's name, code and the subject/course
	if player.OwnedClasses:FindFirstChild(code) then--if the player owns the class then
		members = Work:GetAsync(code)--Check to see if the code is actually a code
		if members then--if it's a code then
			print(#members.Work)--Always prints 0 even if I just sent a remote event and inserted something into the table
			Work:UpdateAsync(code, function(members)
				table.insert(members.Work, subject)--in the work table insert the subject/course
				print(#members.Work)--Always prints 1
			end)
			ReplicatedStorage.AddWorkToClassClient:FireClient(player,subject)--fire a back to the client
			
		else
		end
	end
end

ReplicatedStorage.AddWorkToClass.OnServerEvent:Connect(addWorkToClass)

Thanks and have a good day! :grinning:

You have to return what you want to save in UpdateAsync’s transform function. Just put return members at the end after the print.

3 Likes