Table only returning one object, supposed to return multiple

I’m making custom functions to find objects, and I’ve ran into a problem.

I have a GetSiblings() function, but its not working:

function module:GetSiblings(Object : any?) -- Gets all siblings of a declared object
	
	local SiblingTable = {}
	
	if not Object then
		
		warn("Attempted to GetSiblings() on a nil object")
		
	end
	
	for index, variable in pairs(Object.Parent:GetChildren()) do
		
		table.insert(SiblingTable, variable)
		
		return SiblingTable
		
	end
	
end

Script handling function:

local SiblingFunctions = require(script.Parent.SiblingFunctions)



for i,v in pairs(SiblingFunctions:GetSiblings(workspace.Baseplate)) do
	
	print(i) -- only returns one
	print(v) -- only returns "Camera"

end

Any help?

1 Like

You return “SiblingTable” in the for loop, just take it out of the for loop and you’re done.

Error: ServerScriptService.Script:5: Missing argument to #1 'pairs', table expected

You need to return the table.

From my knowledge, GetChildren() returns a table of children.

What I mean is you need to return the table, “SiblingTable” AFTER the loop

function module:GetSiblings(Object : any?) -- Gets all siblings of a declared object
	local SiblingTable = {}
	
	if not Object then
		warn("Attempted to GetSiblings() on a nil object")
		
	end
	
	for index, variable in pairs(Object.Parent:GetChildren()) do
		table.insert(SiblingTable, variable)
	end

	return SiblingTable
end

Wait, how can I make it so it does not return the Object argument in the table?
Otherwise it defeats the entire purpose of GetSiblings()

oh just add an if statement checking if the “variable” matches “object”

if variable ~= Object then
	table.insert(SiblingTable, variable)
end
1 Like