I'm trying to make a script that would unanchor parts inside a model, which is inside another model. Like this:
To do this, I would use :GetChildren()
c = script.Parent.ELights:GetChildren() for i=1, #c do if c[i].className == ? then c[i].Anchored = false end
Now, the code above is incomplete, as I don't know how to get the children after doing :GetChildren() on the Model which is named "ELights" This is where i ask for help.
You've got the children of your first model "ELights", you would just do another for loop inside the first for loop to get the children in the other models too (or grandchildren, depending on what you want to call them).
Since you're using a numeric for loop, I will just keep to using a numeric for loop as well in the below script.
c = script.Parent.ELights:GetChildren() for i=1, #c do if c[i].Name == "EmergencyLights" then --In case you only want the items in the model named "EmergencyLights" to be unanchored. lights = c[i]:GetChildren() --Get the children of the "EmergencyLights" model. for l=1, #lights do --Repeats the loop l times. l will be the number of children in the "EmergencyLights" model. lights[l].Anchored = false --Unanchor the part. end end
The reason we don't use i in the next for loop is because i is already being used by the first loop. You may also want to add an if statement before unanchoring to check if the child is a part.
There are two types of for loops you can use to loop through all children in the model
for i,k in pairs (model:GetChildren()) do
or
for i=1,#model:GetChildren() do
The first type will be easier to use, but let's start with the second. It seems you understand how the second method works.
model:GetChildren()[1] --child 1 of model model:GetChildren()[2] --child 2 of model
The second method will use the variable 'k' and change it to the child so you dont have to use [1],[2], etc.
k = model:GetChildren()[n] --when the loop goes through the code the nth time k = model:GetChildren()[1] --when the loop goes through the code the 1st time
so in your context, this is how the code would look:
local c = script.Parent.ELights:GetChildren() for i,k in pairs (c) do if k.ClassName == "Part" then --I would use k:IsA("BasePart") but you could use this k.Anchored = false end end
Ask me for more help if needed.
In your for loop, each child is represented by c[i]
, the c counts as all the children that you have, and i is the number of the current child you're unanchoring.
If you undid a for loop, and looked at each individual child, the loop would look like this:
i c 1 A part 2 A different part 3 Another alternative part 4 Once again a different part 5 and so on..
So basically, you've already gotten all the children! If you wanted to find them again after you used this loop, just do the same.
Also, you need another end
between line 5 and 6, you've not closed your if statement.
What I would do is make a function so that the same code can be run inside the same code again. In case you aren't sure what I mean let me show you:
--I will make two scripts. One that gets all the children of the model and put it into a table, and one that makes all the parts unanchored --TABLE CHILDREN SCRIPT local AllChildren = {} --Make an empty table function GetEveryChild(a) --Create a function with the argument "a" for i,v in pairs(a:GetChildren()) do --i is an integer. It's the "Identification Number" for that object. v is the actual object table.insert(AllChildren, #AllChildren+1, v) --I sometimes have problems with this. If it works for you then congratulations GetEveryChild(v) end end GetEveryChild(game.Workspace) --This will fire the "GetEveryChild" function which will completely scan Workspace. --UNANCHORED SCRIPT function UNANCHORALLPARTS(b) --Create a function with the argument "b" for i,v in pairs(b:GetChildren()) do if v:IsA("BasePart") then --If the object is any type of part then... v.Anchored = false --Unanchor the part. end UNANCHORALLPARTS(v)--Scan inside the object for any other parts end end UNANCHORALLPARTS(game.Workspace.Model) --The script will search for a Model in workspace called "Model" and Unanchor any parts inside.