local Camera = workspace:GetChildren().Script
print(Camera.Parent)
COMMUNITY QUESTION
Why wont this work if :GetChildren returns a dictionary?
2 answers
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)
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
This discussion is preserved as it was. New answers, comments, and votes are closed.
