Here is my server-side code:
local PFsearch_RF = game.ReplicatedStorage:WaitForChild("PFsearch_RF") local PFitems = {} function PFsearch_RF.OnServerInvoke(player) PFitems = {} local PF = game.ServerStorage.Inventories:WaitForChild(player.Name) for i,v in pairs(PF:GetChildren()) do if v:isA("Tool") then table.insert(PFitems,v) end end return PFitems end
And here is the client-side code:
local PFsearch_RF = game.ReplicatedStorage:WaitForChild("PFsearch_RF") function backpackRefresh() items = {} local PFitems = {} PFitems = PFsearch_RF:InvokeServer(player) for i, v in pairs(PFitems) do table.insert(items, v) end search(char) -- Ignore these 2 functions, search and refresh, please. They work and I can guarantee they are unrelated to the issue. refresh() wait() end backpackRefresh()
(BTW, player.Name is a server storage folder of tools for each player)
Testing it with print commands on the server side, the array PFitems does contain 1 tool when I run it. However, the client reports that it is empty. I can't see why it won't return properly. Can someone explain what I am doing wrong?
Sorry if I left out some important details, please let me know if so.
RemoteFunctions bind a function as a callback when invoked. To do this, you must use the assignment operator. You are defining .OnServerInvoke as a function rather than assigning one to it. Your client code seems fine, however.
local PFsearch_RF = game.ReplicatedStorage:WaitForChild("PFsearch_RF") local PFitems = {} PFsearch_RF.OnServerInvoke = function(player) --Assign function to bind on invoke PFitems = {} local PF = game.ServerStorage.Inventories:WaitForChild(player.Name) for i,v in pairs(PF:GetChildren()) do if v:isA("Tool") then table.insert(PFitems,v) end end return PFitems end
So I figured out that the problem the whole time was that even though I was sending the table over, the items in it were still in Server Storage, so the local script couldn't read them. The solution was to create remote events and have the server temporary clone the player's folder to replicated storage, have the local script read that temp folder, then destroy it. Creating an extra table, and making a remote function was unnecessary to have the local script read the player folder.