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

How to use make this easier and maybe use GetDescendants?

Asked by 6 years ago
Edited 6 years ago

So far this is my script but does work...I just took a long time to make it and wondering if anything easier. I am still trying to figure it out so I don't have lots of part to name.

I used to named my parts like Part1 Part2 Part3 and then a run a code like this

script.Parent.MouseButton1Down:Connect(function()
    if game.Workspace.Games.Pool.Lights.Value.Value == true then

    game.Workspace.Games.Pool.Lights.BigLight.SurfaceLight.Enabled = false

    game.Workspace.Games.Pool.Lights.Value.Value = false

    game.Workspace.Games.Pool.Lights.LightSectionSeven.PoolLights1.LightPart.SpotLight.Enabled = false
    game.Workspace.Games.Pool.Lights.LightSectionSeven.PoolLights2.LightPart.SpotLight.Enabled = false
    game.Workspace.Games.Pool.Lights.LightSectionSeven.PoolLights3.LightPart.SpotLight.Enabled = false
    game.Workspace.Games.Pool.Lights.LightSectionSeven.PoolLights4.LightPart.SpotLight.Enabled = false
    game.Workspace.Games.Pool.Lights.LightSectionSeven.PoolLights5.LightPart.SpotLight.Enabled = false
    game.Workspace.Games.Pool.Lights.LightSectionSeven.PoolLights6.LightPart.SpotLight.Enabled = false
    game.Workspace.Games.Pool.Lights.LightSectionSeven.PoolLights7.LightPart.SpotLight.Enabled = false
    game.Workspace.Games.Pool.Lights.LightSectionSeven.PoolLights8.LightPart.SpotLight.Enabled = false
    game.Workspace.Games.Pool.Lights.LightSectionSeven.PoolLights9.LightPart.SpotLight.Enabled = false
    game.Workspace.Games.Pool.Lights.LightSectionSeven.PoolLights10.LightPart.SpotLight.Enabled = false
    game.Workspace.Games.Pool.Lights.LightSectionSeven.PoolLights11.LightPart.SpotLight.Enabled = false
    game.Workspace.Games.Pool.Lights.LightSectionSeven.PoolLights12.LightPart.SpotLight.Enabled = false

Is there anyway I can make this easier? If I named them all PoolLights and they would all turn on instead of numbering 100 lights? Like make a model and all the childrens in that model will turn on?

Seems like I am doing something wrong.

0
please put your code in correctly ScrubSadmir 200 — 6y
0
Ok its been put correctly. AcentrixTheYouTuber -8 — 6y

1 answer

Log in to vote
0
Answered by
Vexture 179
6 years ago

The :GetDescendants() function is what you want to use. :GetDescendants() is a function that returns an array of all of the instances located inside of what you want to get descendants from. An array looks like this: {part1,part2,part3}.

If you use :GetDescendants() and want to modify the properties of a specific kind of object, you have to loop through the array with a condition that checks for the type of object. In your case, you want to turn lights on and off. To do this, you want to use a For loop. Here's an example:

for _, v in pairs(workspace:GetDescendants()) do
    if v:IsA("PointLight") then
        v.Brightness = 0
    end
end

This would turn all of your lights off because it sets their brightness to 0.

The For loop goes through your entire array of descendants and using the if statement, checks what kind of instance everything in the array is. If it's a PointLight, it turns it off.

This is the basic concept. Hope it helps!

Ad

Answer this question