Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to get all descendants of workspace?

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.

2 answers

✓ Accepted answer

Recursion

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)

3 comments

0

Ah. That is a way I hadn't even thought about. Thanks for the answer, Digital. I just learned some new things.

0

No problem. If I helped, remember to upvote and hit that 'accept answer' button! :D

0

Both of you tried, but I just added some more for loops and it is working. Thanks for the help from you both. I would go with yours, but I didn't really want to make a function.

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.

4 comments

0

As I thought, it didn't work since all you did was pretty much complicate the original script. I did test it. I have an idea on how to make it work though, it will be a bit more complicated. Thank you for trying though.

0

Huh. I thought so as well, I honestly have not a clue. Digital did a much better job, and my way, as you mentioned, just over complicated.

1

The reason your solution may not work is because you're only looping through things named 'Rail' that are DIRECTLY under workspace. If these 'Rail' instances were in models under workspace though, they wouldn't be checked. Otherwise this was pretty close :P

1

Ah I see. Makes sense. Didn't really consider they could be all over the place in different models.

This discussion is preserved as it was. New answers, comments, and votes are closed.