Can't use TeleportService:ReserveServer inside UpdateAsync: "Transform function error Callbacks cannot yield"

local DataStoreService = game:GetService("DataStoreService")
local MyServersStore = DataStoreService:GetDataStore("MyServers")
local TeleportService = game:GetService("TeleportService")
MyServersStore:UpdateAsync('Server1', function(MyKey)
    MyKey = TeleportService:ReserveServer(7013735989)
    print(MyKey)
    return MyKey
end)

If I try to use TeleportService:ReserveServer inside GlobalDataStore:UpdateAsync I get:

image

Despite the error, it creates the key. But it doesn’t save it.

How to create a PrivateServerId with ReserveServer inside UpdateAsync?

Sorry for the extremely delayed response, but I will answer this question for posterity and any future searchers.

The reason the error Transform function error Callbacks cannot yield is being shown is due to the fact that you cannot call yielding functions inside UpdateAsync, and the fact that it does not save is likely a result of this. TeleportService:ReserveServer is a yielding function.

It seems like what you are trying to do is reserve a server, then update the datastore of Server1 to be value of the server’s access code. In that case, you could call TeleportService:ReserveServer outside of the async function, hold the result in a local variable, then just return that local variable value inside UpdateAsync. Ideally this would all be contained in its own function so that the access code doesn’t leave its scope. It would look something like the following:

local DataStoreService = game:GetService("DataStoreService")
local MyServersStore = DataStoreService:GetDataStore("MyServers")
local TeleportService = game:GetService("TeleportService")

function saveKeyAndUpdateStore()
  local reserveCode = TeleportService:ReserveServer(7013735989)
  MyServersStore:UpdateAsync('Server1', function()
      return reserveCode 
  end)
end

saveKeyAndUpdateStore()
8 Likes