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

Using getchildren on a model returns a weird table in the output?

Asked by 5 years ago

so heres what i did i wanted to print all the children in a model

so i did

ModelChilds = workspace.Xmodel:GetChildren()
print(ModelChilds)

ModelChilds:Destroy()

But the ModelChild was not printed nor destroyed

but for the print it returned a output saying table: 0x600009035630

4 answers

Log in to vote
0
Answered by 5 years ago

It's not weird!

It's the table's memory address! If you want to print each child name traverse the table returned or use unpack()

local ModelChilds = workspace.Xmodel:GetChildren();

for _, child in ipairs(ModelChilds) do
    print(child); --// print each child name
end

--// or \\--
print(unpack(ModelChilds));

You are also trying to call a nonexistent function :Destroy() on the table itself and not the children.

Just use :ClearAllChildren() if you need to delete just the children

workspace.Xmodel:ClearAllChildren();
Ad
Log in to vote
0
Answered by 5 years ago

The Name From what research I did, I found that that's the hex for the table, based upon this question on stackoverflow.

The Code When you want to destroy children that're in a table, you need to use a for loop to iterate through said table. A for loop (I'm speaking of a generic for loop), it returns two values: the index, and the value for said index.

local ChildrenInModel = Model:GetChildren()
for _, v in next, ChildrenInModel do
    print(v.Name, 'is in table!')
end

Side note; when attempting to use a method on a table, such as Destroy, it can look pretty weird when you look at it like this;

local ChildrenInModel = {Part1, Part2, Part3, etc...}

ChildrenInModel:Destroy()

-- Another way to look at it is

{Part1, Part2, Part3, etc...}:Destroy()

Stuff touched on

  1. For loops - A loop that counts up or down depending on the values given to it.

  2. Generic For loops - A for loop that iterates through a table that returns the index for its first value, and the value for the index. (I recommend reading up on it for a better explanation.)

  3. Destroy - Pretty self explanatory. :p

If you have any questions, feel free to ask. :) I hope this helped! :D

0
ipairs nerd User#24403 69 — 5y
Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

GENERAL PRACTICE

Use :WaitForChild() to make sure a part / model exists before it is defined or changed

Use local rather than global variables as they are more versatile. (ex. "local model =" instead of "model =")

ISSUES

In order to destroy all of the children of "Xmodel" you need to iterate through the table using a for-loop (in pairs automatically gives you the instance of the child)

The print of a table will not return the children instances, but instead will give the actual table instance you've created.

REVISED SCRIPT

for i, v in pairs(workspace:WaitForChild("Xmodel"):GetChildren()) do
    v:Destroy()
end

You can also use :ClearAllChildren() however, any connections you have regarding these children will be kept, and not disconnected, so :Destroy() would be preferred.

0
whats the point of 'i' then Zottic 19 — 5y
0
"i" is the index of the table, it's the same as the numerical position of the item, so if I have a table {"one", "two", "three"} then the index for the value "two" is 2. Another way to write the the keyvalue is table[1] since [1] is the index of the item in the first slot of the table SerpentineKing 3885 — 5y
Log in to vote
-3
Answered by
Zottic 19
5 years ago
Edited by User#24403 5 years ago

The thing is that Lua doesn't even know what you want to do with the children. You're saying that you want it to get the children and print it. How are you supposed to print children? Lua will just return.

I have just the right solution to this problem, you want the script to 'Type' the children. So you need to copy what I write.

ModelChilds = workspace.Xmodel:GetChildren()
print(type(ModelChilds))

ModelChilds:Destroy() -- you are destroying the children

I put a 'type' there so the script types the string, not the ID

Answer this question