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

Why wont this work if :GetChildren returns a dictionary?

Asked by 4 years ago
local Camera = workspace:GetChildren().Script

print(Camera.Parent)

2 answers

Log in to vote
1
Answered by
Psudar 882 Moderation Voter
4 years ago

The reason you can't do this, is because it actually returns an array.

Arrays are tables that have numerical keys.

So, you can't index an array with a string.

What you can do however, is create your own dictionary.

I wrote a short script for ya.

local childrenArray = workspace:GetChildren() --get the array

local function createDictionary(array) --this function takes an array as an argument
    local newDictionary = {} --create a new table
    for i, v in pairs (array) do --loop through given array
        newDictionary[v.Name] = v --name of the object is the key, actual object is the value
    end
    return newDictionary --after loop, return it
end

local childrenDictionary = createDictionary(childrenArray) --now call the function here

print(childrenDict.Baseplate.Parent)  
Ad
Log in to vote
-1
Answered by 4 years ago

I'd Recommend you get familiar and tweak a bit with this piece of code

local Test= workspace:GetChildren()
for _,v in ipairs(Test) do
    print(v.Name)
end

if I would have translated your script though, something like this would be the result

for _,v in ipairs(workspace:GetChildren()) do
    if v.Name == "Script" then
        print(v.Parent.Name)
    end
end

Answer this question