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

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

Asked by
Peeshavee 226 Moderation Voter
8 years ago
Edited 8 years ago

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!

0
Pay close attention to your error messages. ScriptGuider 5640 — 8y

2 answers

Log in to vote
1
Answered by 8 years ago
Edited 8 years ago

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
0
Well, now the error is gone, but nothing works. No other errors are given. Peeshavee 226 — 8y
0
Maybe line 8 should be BackgroundColor3? OldPalHappy 1477 — 8y
Ad
Log in to vote
0
Answered by 8 years ago

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.

Answer this question