Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why is GetChildren() is only obtaining one child instead of all of them?

Asked by 9 years ago

I made a script with a SurfaceGui in it. The script is located in a Model with tons of Parts all named Screen.

What I want to do is for the script to replicate the SurfaceGui into all the Parts at once, however the problem is that it only replicates once, and in only one particular Screen instead. Do I need to add a while loop or something?

--

rep = script.SurfaceGui:Clone()

for i,v in pairs(game.Workspace.InformationScreens:GetChildren()) do if v.Name == "Screen" then rep.Parent = v end end

2 answers

Log in to vote
1
Answered by 9 years ago

Please provide explanation with your answers. Simply posting code does not spread knowledge of integral scripting processes which helps people understand the logic and reasoning behind your answer.

You never cloned the gui inside of the loop. So here you go:

for i,v in pairs(game.Workspace.InformationScreens:GetChildren()) do if v.Name == "Screen" then 
rep = script.SurfaceGui:Clone()
rep.Parent = v
end end
0
It works! Thanks a lot!! ChromeNine 50 — 9y
Ad
Log in to vote
-3
Answered by 9 years ago

Your :GetChildren() script probably did not use a loop what :GetChildren() does is get all the children but, it doesn't allow you to access each of them separately to do that you would use a for loop something like this...

local children = script.Parent:GetChildren()
for i = 1, #children, do
wait()
local gui = script.SurfaceGui:clone()
if children[i].Name == Screen then
gui.Parent = children[i]
end
end

This will go through every single object inside of the model and it will clone the gui every time the for loop runs and parent it to whatever 'i' is and since it's a for loop it's changing value every time it loops..

Answer this question