Am I able to make one variable equal multiple parts?

Trying to explore new but simpler options with lua variables.

Question: Am I able to route one variable to equal multiple parts?

For example:

Lights = game.Workspace.Model1.Light or game.Workspace.Model2.Light

Why doesn’t this work? Are there any similar approaches or workarounds? I’d prefer something similar to this. I’m exploring ways keep most scripts within the ServerScriptService for security.

Thank you.

simple answer no but you can use tables and store multiple parts in the table.

local Lights = {game.Workspace.Model1.Light,game.Workspace.Model12.Light}

--// Get a specifc light
Lights[1] -- This would be Model11

--// Get a random light
local RandomLight = Lights[math.random(1,#Lights)] -- This will return a random light in the table
1 Like

Do you mean using arrays?

local Lights = {workspace.Model1.Light, workspace.Model2.Light} --lua uses brackets for arrays

for _, light in pairs(Lights) do --looping through each light inside the array
	print(light, light.Parent)
end
2 Likes
local MyArray = {}

for _, v in ipairs(game.Workspace.Lights) do -- Make sure all the lights are in a FOLDER!
    table.insert(MyArray, v.Name)
end

for _, v in ipairs(MyArray) do
    print(tostring(v))
    table.remove(MyArray, v)
end

return MyArray

ModuleScript, LocalScript, OR normal script, whichever works best for you! Make sure to parent all the lights you’d like inside a FOLDER, or it won’t work!

1 Like

Is there a way to grab multiple with the Lights[1] ?

If you wanted to grab multiple light objects in a table, then you would want to use a for loop to loop through the table and grab all the lights you want, then do whatever you’re gonna do to them.

Alternatively, you could just list each index in the table like:
Lights[1]
Lights[2]
Lights[3]
etc.
This would probably be extremely inefficient however, especially if you had like 20 lights in the table, so a loop would just be better.

You could use a for loop to go through all the parts named light for example

for _, part in pairs(game.Workspace.Model1:GetDescendants()) do
	if part:IsA('Part') then
		if part.Name == "Light" then
            --do the thing you want it to do      
        end
	end
end

Dunno if everything i wrote is correct as i just wrote it by memory so you might have to check dev guides on for loops but this is one way to apply effects to all parts with same name or something.

2 Likes