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!
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
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.