Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

1 comment

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.

1 answer

✓ Accepted answer

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"

1 comment

0

nice rep thievery

This discussion is preserved as it was. New answers, comments, and votes are closed.