Hello,
I have been searching far and wide for something related to what I’m asking for but I cannot seem to find it. I am trying to figure out how to make a script that will:
Looks through every single part in workspace, regardless of how far the parent/children tree goes
If a part has a specific name, cut and paste the part into a specific model in workspace
Basically think of it as opening a bunch of opened and closed boxes, picking the right colored skittles color, then placing it into a plastic bag for future use.
The reason for this is because on the game I am working on, there are item spawns that are anywhere in the workspace, and I want to make a script that will organize everything once the server begins. This way, I can build whatever I want and paste it into the map without having to worry about moving everything around.
Hopefully this makes sense. If anything needs to be clarified let me know.
You can always use :GetDescendants() for that one. Does exactly what you want.
Then all you’d gotta do is work out the logic for identifying the parts you want.
And if you’re looking for something that’ll detect parts as they’re added, you can use workspace. DescendantAdded:Connect(FunctionNameHere)
You’re in luck because there is an easy way to do this!
Use workspace:GetDescendants() to get all instances within workspace in a table.
Use a for KEY, VALUE loop to iterate and manipulate objects in that table:
for key, value in pairs(workspace:GetDescendants()) do
if value.Name == "WhateverName" then
local clone = value:Clone()
clone.Parent = workspace.Model --your model that you wanted to copy/paste to
value:Destroy() --if you want, you don't need to destroy the instance if you don't want to. I just did that to avoid too much redundancy of having multiple identical objects
end
end
@ShadokuSan@TheCarbyneUniverse
OMG I saw getDescendants and thought it only applied to just the first layer of the parent/children tree. That is really really useful, thank you so much!
Just remember:
Descendants mean every object within an object (from children to children’s children and so on…). Think of it like how we are descendants of cavemen.
Children are still considered descendants, however, only for the first layer. Children are special types of descendants just like how squares are special types of rectangles. We are certainly not children of cavemen because their relationship with us is not direct (thank goodness …).
Both GetChildren() and GetDescedants() follow the principles above that correspond to them.
That answers my question. At first, I actually thought getDescendants was just a depricated version of getChildren, but it makes sense now.
How would getDescendants respond in regards to items that are not on the bottom of the tree? Such as if I took the second to bottom descendant, cloned it to another location, then destroyed it, would the descendants of the model no longer be considered within the for loop? (Hopefully that makes sense)
If you basically cut and pasted some descendants to a different parent, would they still be considered descendants of the original parent?
Very interesting question indeed, because there are two answers. Simple answer: no. Elaborated answer: yes and no.
Remember how I called :GetDescendants() on workspace? Well, that returns a table of all objects in workspace and stores it in the script until it’s overridden or nullified.
For example, a variable can store this table:
local des = workspace:GetDescedants() --stored
But then, if you were to move some descendants, they would still be in the stored table above (because we didn’t change it). However, if you were to create a new table by calling this function, then the relocated descendants are NOT included.
--move descendants
workspace.Child.GrandChild.Parent = Lighting
workspace.Child.Sister.Parent = ReplicatedFirst
local newDes = workspace:GetDescedants() --withOUT Sister and GrandChild
--[[
However, the variable from before "des" still has Sister and Grandchild because it was created before we relocated them.
]]
Yes, you understood me correctly.
So basically, I would make a variable that gets the descendants before the for loop, then if the item is noticed to be a model with a name that is desired then copy the model over to another location, destroy the original model, then re-define the variable again with another “workspace:GetDescedants()”?
Thank you! I got it to successfully move parts. Then I was also able to make it move models with:
--Move doors
local dec = workspace:GetDescendants()
for key, value in pairs(dec) do
if value.ClassName == "Model" and value.Name == "DOORMODEL" then
local clone = value:Clone()
clone.Parent = script.Doors --your model that you wanted to copy/paste to
value:Destroy() --if you want, you don't need to destroy the instance if you don't want to. I just did that to avoid too much redundancy of having multiple identical objects
dec = workspace:GetDescendants()
wait()
end
end
Had to add a wait due to the constant getdescendants causing lag every time it looped.