Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why isn't my Remote Function returning its array?

Asked by 3 years ago
Edited 3 years ago

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.

2 answers

Log in to vote
0
Answered by
Sparks 534 Moderation Voter
3 years ago

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
0
Thanks for the reply. I changed my code to this, but it still returns an empty table, though.. BabyDabey 0 — 3y
0
Because in your server code, PFitems is always empty when you set it to {}, so you are returning an empty table. Sparks 534 — 3y
0
Actually, I figured out the issue. It had nothing to do with my code, I'll elaborate in the "Answer". BabyDabey 0 — 3y
Ad
Log in to vote
0
Answered by 3 years ago

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.

Answer this question