SetAsync is Yielding

function _G.SaveData(Player)
if Player:FindFirstChild(“StorageFile”) and Player:GetAttribute(‘Saved’) == nil then
Player:SetAttribute(‘Saved’,true)
local StorageFile = Player.StorageFile

	local DataScope = 'TGData'.. _G.ScopeV1 ..Player.UserId ..'s'.. StorageFile.Customization.Team.Value
	
	local DataStore = DataStoreService:GetDataStore("Data",DataScope)
	local NewData = {}		
	
	for i,v in pairs(DataOrder) do
		table.insert(NewData,StorageFile[DataOrderTypes[i]][v].Value)
	end
	print('saving')

	DataStore:SetAsync(DataScope,NewData)
	print('saved')

end

end

This script only happen at once and also i’ve tried pcall but it is not printing the error

This bug is not occuring before maybe its because i update roblox studio.

Expected behavior

What i expect is to save the data when i leave but in my scenario this setasync is not instantaneous to save the data

1 Like

SetAsync yielding is intended, check the docs. Did you mean that it’s yielding permanently?

not permanently but only once when this setasync occur to save

It seems it’s yielding where you call :WaitForChild on StorageFolder which tells me two things:

  1. You are either waiting for something that doesn’t exist at all,
  2. You are trying to wait for something too early that doesn’t exist yet

Can you find where you’re waiting for the Folder and then make sure that folder is being created where you’re waiting for it in the actual explorer?

i mean that is unnecessary my problem is only why setasync is not intend to work when i leave also it is not sending error even i try the pcall

1 Like

It’s not unnecessary, you have a warning telling you that there is an infinite yield on waiting for the StorageFolder. That is your issue not SetAsync, fix that, and you’ll fix the problem.


it is not printing the ‘saved’ because the setasync is still yielding

@colbert2677 can maybe help you a little better, as I am not an expert in this field

is your setasync is working?? because i’ve tried another script like this and still not working

local DataStoreService = game:GetService(“DataStoreService”)

function Save(Player)
local DataScope = Player.UserId

local DataStore = DataStoreService:GetDataStore("Data",DataScope)
local NewData = {}		


DataStore:SetAsync(DataScope,NewData)
print('saved')

end

game.Players.PlayerRemoving:Connect(function(Player)
Save(Player)
end)

1 Like

and still not printing the ‘saved’

This is likely related to shutdown in Studio not the behaviour of SetAsync (and isn’t a bug).

Use :BindToClose:

game:BindToClose(function()
	for _, player in Players:GetPlayers() do
		Save(player)
	end
end)

BindToClose’s callback will prevent the game from stopping for up to thirty seconds while the code in the callback completes. What is happening is you are hitting the stop button, which shuts down the internal server, and the thread spawned for the PlayerRemoving callback is killed once it begins yielding.

is this including the player who leave ?

I don’t really understand the question… The problem though is that SetAsync is being interrupted by the server shutting down, so you need to use BindToClose to save player data as well.

Thanks! i think i misinterpret the bind to close

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