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

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

Asked by 4 years ago
Edited 4 years ago
 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

Log in to vote
1
Answered by
Elixcore 1337 Moderation Voter
4 years ago

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).

Ad

Answer this question