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

function that returns getchildren in workspace will only return one thing?

 function ReturnWorkspace()
	for i,v in pairs(workspace:GetChildren()) do
		return {Index =  i , Value = v}
	end
end


print(ReturnWorkspace().Index) -- Only prints 1 and prints Camera if I do v

-- Since I have 6 things in workspace it should return 6 for index but its returning only 1

Why does this not return everything in workspace

it only returns camera and prints it for some reason?

Anyone know a fix to this?

So I can return every single thing in workspace

1 answer

Whenever you use return, the entire function stops. that's how return functions, so after 1 iteration your entire function stops and returns the current value.

What you should do instead is something like this, which will return all of them.

 function ReturnWorkspace()
	local tab = {}
    for i,v in pairs(workspace:GetChildren()) do
        tab[i] = v
    end
	return tab
end


print(ReturnWorkspace()[1])

This will return all of the workspace's children with their respective index (numbers) and values (objects).

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