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

RemoteFunction returns only 1 value when I did :Getchildren() iv loop?

Asked by 4 years ago

Server Script

local remotefunction = game.ReplicatedStorage.RemoteFunction

local ServerStorage = game.ServerStorage



remotefunction.OnServerInvoke = function()
    for i,v in pairs(game.ServerStorage:GetChildren()) do
        return v.Name -- should return a table 
    end


end


Local Script

local remotefunction = game.ReplicatedStorage.RemoteFunction


local ValuesGot = remotefunction:InvokeServer()

print(ValuesGot)-- Only prints one intvalue 


print(unpack(ValuesGot)) --Should be a table but it errors saying its a string

The local script should Return the name of the int values which should return the names of 3 intvalues in server storage but its only returning 1

0
The reason only one value is being returned is because when you use return, it ends the loop. Consider creating a table beforehand and using table.insert. Unhumanly 152 — 4y

1 answer

Log in to vote
1
Answered by 4 years ago

It's not returning a table.

Once a function reaches a return statement, it stops and returns whatever you said it should return. In this case, the function will only return the name of the first object in the table.

What you should do, is actually return a table of the names.

local names = {} 
for _, child in ipairs(game:GetService("ServerStorage"):GetChildren()) do
    table.insert(names, child.Name) 
end
return names

Also, the term for that type of loop is generic for, not "iv loop"

0
nice rep thievery Unhumanly 152 — 4y
Ad

Answer this question