So. I have a script. Only one part wrong with it.
tab= workspace:GetChildren() -- This line causes the problem. But there is also no error. for i, v in pairs(tab) do if v.Name == "Rail" then v.Script:Destroy() script.changer:Clone().Parent = v v.changer.Disabled = false print('New Script Done.') end end
It is supposed to add a new script to every part named "Rail" because I am lazy and i found a mistake and i don't want to go through each individual piece and fix it. Anyway, I found the reason why it isn't working, which is the fact that each of those pieces is in a model , in a model, in workspace, and I cannot get the to the rail piece because :GetChildren() does not work that way. Is there another method that can go in place of :GetChildren() that allows me to get all of the instances in workspace? Thank you and have a nice day.
Perhaps try looping through those models as well!
You are correct. Using :GetChildren()
will only return a table of objects directly under Workspace. That is why we can make a function to loop through Workspace, and it's descendants looking for everything named 'Rail'
We can use a recursive method to do this:
function insertRail(loc) for _,v in pairs(loc:GetChildren()) do if v.Name == "Rail" then v:FindFirstChild("Script"):Destroy() local sctClone = script["changer"]:Clone() sctClone.Parent = v sctClone.Disabled = false print("New Script Done At: " .. v:GetFullName()) else insertRail( v ) end end end insertRail(workspace)
Hey there Rollercoaster,
I don't exactly know what the issue is but let's try to go over it and fix some potential bugs.
Now, I am not exactly 100% sure on line 1, and I know sometimes it is said that using 'game' isn't required, but I, personally, like to put it in just for my own safety.
So now we have:
tab = game.Workspace
We won't put the GetChildren right now, just because we can do it later down the road. This may or may not be the issue, I am not 100% sure.
Let me just put the code and we can then go through it.
tab = game.Workspace -- Setting a variable for Workspace, not really needed though for _,v in pairs(tab:GetChildren()) do -- We could just put it in here. That's okay though. if v.Name == "Rail" then -- Getting everything that is named Rail for _,i in pairs(v:GetChildren()) do -- Now we are going to into that Rail part if i:IsA("Script") then -- If the decendants of the Rail is a script i:Destroy() -- Destroy it. end local newS = script.changer:Clone() -- Getting the new script newS.Parent = v -- Placing it. newS.Disabled = true --Give it a quick reset. newS.Disabled = false end end end
Please comment if you have any further questions, or if anyone else can help improve the information given, or help clarify.
I personally recommend Digitals way, it is much more efficient than the way I mentioned.