Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

UpdateAsync() transformation function not working, unsure of why?

I currently have a script set up to Update players data while checking Version IDs to prevent any incidents of potentially outdated data being saved.

function SaveMoney(plr,Data)
	if Data then
		local success,err = pcall(function()
			local Key = "Money_"..plr.UserId
			print("Save Function Called") -- To show it does indeed reach this point.
			moneydataStore:UpdateAsync(Key, function(oldData)
				print("Saving Data") -- Does not ever get printed.
				local formerdata = oldData or {DataId = 0}
				print(formerdata)
				if Data.DataId == formerdata.DataId then
					local newData = Data
					return newData
				else
					return nil -- Cancels the save if the data is outdated
				end
			end)
		end)
		print(success,err) -- Does not ever print an output of the pcall.
	end
end

However, it never gets through the pcall, it seems to get stuck in an infinite yield. It never even does the print immediately after the UpdateAsync is called. I would appreciate any help in understanding what is behind this issue.

Update: It appears to be the issue is related to the pcall, as removing it fixes the issue. However, you are always supposed to wrap UpdateAsync in a pcall, so I can't really do that.

1 answer

Code:

function SaveMoney(plr,Data)
	if Data then
		local Key = "Money_" .. plr.UserId
		moneydataStore:UpdateAsync(Key, function(oldData)
			local formerdata = oldData or {DataId = 0}
			if Data.DataId == formerdata.DataId then
				local newData = Data
				return newData
			else
				return nil
			end
		end)
	end
end

Remove the pcall in functions you will call later, and add the pcall before using the function. Example (when autosaving data for multiple players):

while true do
	for _, player in pairs(game.Players:GetPlayers()) do
		local success, err = pcall(function()
			coroutine.wrap(SaveMoney)(player, ...) -- replace "..." with your data to save
		end)
		print(success, err)
	end
	wait(300) -- wait 5 minutes as autosave intervals
end

Another example (when saving data on player leaving and on game shutdown):

-- PLAYER LEAVING CODE:
game.Players.PlayerRemoving:Connect(function(player)
	local success, err = pcall(SaveMoney, player, ...) -- replace "..." with data to save
	print(success, err)
end)

-- GAME CLOSING CODE:
game:BindToClose(function()
	for _, player in pairs(game.Players:GetPlayers()) do
		local success, err = pcall(function()
			coroutine.wrap(SaveMoney)(player, ...) -- as always, replace "..." with data to save
		end)
		print(success, err)
	end
end)

Hope this helps!

1 comment

0

This did not fix the issue, it still does not run the transformation issue. As long as a pcall is involved, the transformation function does not execute.

This discussion is preserved as it was. New answers, comments, and votes are closed.