Wait for :InvokeServer() to return a value

I have a problem, I run :InvokeServer() on a remotefunction, like this:

local itemname = "Chopin"
local specificItem = itemrem:InvokeServer(itemname)
if specificItem then
	print("yup got it")
	print(specificItem)
end

the remote function is hooked like this:

local function item(player, v) 
	return Mod.GetItemByName(v)
end

itemrem.OnServerInvoke = item

and then the module script runs this:

function ShopModule.GetItemByName(v)
	local itemTable = {}
	for _, item in ipairs(shopItems) do
		if tostring(item.name) == tostring(v) then
			print("found")
			table.insert(itemTable, {
				Name = item.name,
				Type = item.itemtype,
				Description = item.description,
				Price = item.price,
				Object = item.object,
				Title = item.title,
				TitleColor = item.titlecolor,
				RotationAngle = item.rotationangle
			})
		end
	end
	print(itemTable)
	
	return itemTable
end

every print except the ones that actually contain the value are printed, which includes

if specificItem then
	print("yup got it")
	print(specificItem) --  <-not this one though
end

but the specific item i requested does NOT get printed out, however if i add a simple wait(1) before checking if specificItem exists, the problem dissapears

so my question is - how do i wait for invokeserver() to completely return the table?

1 Like

just do

repeat task.wait() until specificItem

And I suggest you to have a system of handling things asynchronously and prevent some memory leaks if you’re going to use RemoteFunction

1 Like

this does not solve my issue, as specificItem already exists BEFORE it has a value somehow?
(also :InvokeServer() automatically yields until a value is returned iirc)

i check if specific item exists, and it says it does (it prints out the statement)
however it does NOT print out specificItem, and if i try using specificItem.Title it will just print out “nil”

i fixed it myself
my initial hypothesis was wrong, roblox just doesn’t support tables like this passing through remotefunctions:
{
“name” = a
“type” = b
“price” = c
}

so instead, i had to ‘flatten’ the table to only return {a,b,c} and then handling the logic from there :+1: