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
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();
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
For loops
- A loop that counts up or down depending on the values given to it.
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.)
Destroy
- Pretty self explanatory. :p
If you have any questions, feel free to ask. :) I hope this helped! :D
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.
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