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

Why does this script now work? Output says "GetChildren is a nil value"

So, I'm trying to make a brick that changes color very slowly using Color3.fromRGB. In order to do this, I needed to add SurfaceGui's onto each side of the part, and then add frames into those Gui's. So, I need to GetChildren() more than once. I've asked something similar to this before, but I'm completely confused on how it works. Here's my code:

local part = script.Parent
local model = part:GetChildren()

while true do
	for i,v in pairs(model:GetChildren()) do
		if v.ClassName == "Frame" then
			for b = 1,255,1 do
				v.Color3 = Color3.fromRGB(255,i,255)
				wait(0.1)
			end
		end
	end
end

All help is appreciated!

1 comment

0

Pay close attention to your error messages.

2 answers

You're using :GetChildren on a table.

local part = script.Parent
local model = part:GetChildren()

while true do
    for i,v in pairs(model) do-- Removed GetChildren
        if v.ClassName == "Frame" then
            for b = 1,255,1 do
                v.Color3 = Color3.fromRGB(255,i,255)
                wait(0.1)
            end
        end
    end
    wait()-- I recommend this.
end

2 comments

0

Well, now the error is gone, but nothing works. No other errors are given.

0

Maybe line 8 should be BackgroundColor3?

The :GetChildren() function returns the children of an object in a neat, organized table.

foo = game.Lighting:GetChildren()

for _, v in pairs(foo:GetChildren()) do --outputs error
	print(v.Name)
end

for _, v in pairs(foo) do --says children of lighting
	print(v.Name)
end

If you try to use :GetChildren() on a table it doesn't work, because such a function doesn't exist. On line 5, you just turn ... model:GetChildren() ... to ... model ...

Also, if you wanted to get the children of the children, you wouldn't do :GetChildren():GetChildren(). This would, again, output an error. Inside the for, you would get the children of the child variable. An example is shown below.

for _, v in pairs(workspace:GetChildren()) do
	local children = v:GetChildren()
	for _, g in pairs(children) do
		if g:IsA("BasePart") then
			print("foobar") --prints 'foobar' if the child of the child is a descendant of BasePart
		end
	end
end

Hope that fixes your problem, Iplaydev.

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