Hello, I would like to know how I could get the children of a GetChildren()
In my game, I have a model that contains parts containing pointlights. What I would like to do is set PointLight.Enabled to false using a for loop, however I am unsure how I would reference all of these pointlights?
Here is my current script:
(The model containing the parts that contain pointlights is "Model" - top line)
01 | local parts = game.Workspace.Model:GetChildren() |
02 | local power = game.Workspace.Value |
03 |
04 | while true do |
05 | wait( 1 ) |
06 | if power.Value > 0 then |
07 | power.Value = power.Value - 1 |
08 | if power.Value = = 0 then |
09 | for i, v in pairs (parts) do |
10 | v.Material = Enum.Material.SmoothPlastic |
11 | end |
12 |
13 | else |
14 | for i, v in pairs (parts) do |
15 | v.Material = Enum.Material.Neon |
16 | end |
17 | end |
18 | end |
19 | end |
GetChildren() returns only the first children of a parent. GetDescendants(), on the other hand, returns the children of the children as well. If you want to use the value like a timer which turns lights off when it reaches 0, and whenever someone makes it a negative value the lights come on, try this.
01 | local descendants = game.Workspace.Model:GetDescendants() |
02 | local power = game.Workspace.Value |
03 |
04 | while true do |
05 | wait( 1 ) |
06 | if power.Value > 0 then |
07 | power.Value = power.Value - 1 |
08 | elseif power.Value = = 0 then |
09 | for i = 1 , #descendants do |
10 | if descendants [ i ] :IsA( "BasePart" ) then |
11 | descendants [ i ] .Material = "SmoothPlastic" |
12 | if descendants [ i ] .PointLight then |
13 | descendants [ i ] .PointLight.Enabled = false |
14 | end |
15 | end |
If you simply want a loop to turn off any PointLights in a model, try this:
1 | local descendants = game.Workspace.Model:GetDescendants() |
2 | for i = 1 , #descendants do |
3 | if descendants [ i ] :IsA( "PointLight" ) then |
4 | descendants [ i ] .Enabled = false |
5 | end |
6 | end |