Making a local table for objects?

Ok, so say I have a lot of lights, and I don’t want to individually toggle enabled/disabled on them.

How would I compile all of the lights into a single local variable?
I’ve tried things like this:

local Lights = script.Parent.Light1,script.Parent.Light2 (etc etc)

And something similar to this:

local Lights = {
script.Parent.Light1,
script.Parent.Light2
}

Neither of those worked, I don’t need a full script, just a way to make the table.

1 Like

I just put them in a folder in workspace and use a GetChildren for loop to toggle lights not tables or anything

1 Like

This works fine, I don’t know what your problem with this would be.

2 Likes

I can’t try any of this as of now, but I’ll try it as soon as I can and tell you what I get.

You can work with a table, but you’ll have to run a for loop.

local Lights = {
    script.Parent.Light1,
    script.Parent.Light2
}

for _, light in next, Lights do
    print(light)
end

-- or

for _, light in pairs(Lights) do
    print(light)
end
1 Like

Put all your lights into a folder, then put all the objects into a table.

local lights = {} -- list of our lights


for i,light in pairs(LightsFolder:GetChildren()) do -- run through all the children of the lights folder, add them to our list
table.insert(lights, light) -- add the light
end

-- now the "lights" table will consist of all the lights that were in the folder
3 Likes

Since you’re looking to easily manage groups of instances, the CollectionService would work perfectly for you! This service allows you to manage groups of instances by setting tags on individual instances.

Using the AddTag method, you can add a tag to an instance like so:
game:GetService('CollectionService'):AddTag(instanceToSetTagFor, tagName)

Using the GetTagged method, you can get a table of objects with the tag like so:
game:GetService('CollectionService'):GetTagged(tagName)


@Sweetheartichoke has made a very nice plugin to visually manage tags in Studio. This would make it very easy to add the “Lights” tag to all your light instances in Studio.

Once you set the tags for the lights, you can easily go through the entire table to enable/disable them.

local Lights = game:GetService('CollectionService'):GetTagged('Lights')

for _, light in pairs(Lights) do
   light Toggle.Value = not light.Toggle.Value
end
8 Likes

Isn’t this code equivalent to local lights = LightsFolder:GetChildren()?

3 Likes

Yeah, but the above code allows for easy addition of filters.

like

for _, light in pairs(Lights) do
if light.Name == "Light" then
   light Toggle.Value = not light.Toggle.Value
end
end

Sorry if this is an obvious question, but does it have to be a folder or can I have it as a model?

In short, yes.

All instances can be indexed and they have the GetChildren method built-in allowing you to get a list of the instance’s children.

Now these are all mixed answers but to be straight to the point, you can simply use :GetChildren() on a folder/model/anything parenting the objects with all the lights in them. To toggle them just loop through and enable or disable them that way.

This is probably the easiest and simplest way to do this, and should hopefully suit your needs.

2 Likes