Do I Need A Table? How Do I Use One?

  1. What do you want to achieve? Keep it simple and clear!
    I am tryin to simplify my script and make it more effective.

  2. What is the issue? Include screenshots / videos if possible!
    The issue is I am not sure how I can make this script shorter? I do not know how to search for parts with specific names without typing them out one by one in the script.

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I have considered that a table would help, but it looks to me tat I would still have to type out the name of each part into the script.

Here is my script. When I click on a screen gui, it searches for these mesh parts (my lights) inside of the folders they are organized. For this specific script I only want to find specific parts to change their color and material.

Here is a visual in case it helps. I named the parts based on their row/column position! The blue dots are the parts I typed out in my script (.i.e. “R1C1”).

local LightBulbs = game.Workspace.Lights["Light Bulbs"]
local TweenService = game:GetService("TweenService")
local Info = TweenInfo.new(2, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut, -1, true, 0)

script.Parent.MouseButton1Click:Connect(function()		
	for i,v in pairs(LightBulbs:GetDescendants()) do
		if v.Name == "R1C1" or	v.Name == "R1C3" or v.Name == "R1C5" or v.Name == "R1C7" or v.Name == "R2C2" or v.Name == "R2C4" or	v.Name == "R2C6" or	v.Name == "R2C8" or v.Name == "R3C1" or	v.Name == "R3C3" or v.Name == "R3C7" or v.Name == "R4C2" or v.Name == "R4C6" or	v.Name == "R4C8" or v.Name == "R5C1" or	v.Name == "R5C3" or v.Name == "R5C7" or v.Name == "R6C2" or	v.Name == "R6C6" or	v.Name == "R6C8"
		then
			v.Material = Enum.Material.Neon
			TweenService:Create(v, Info, {Color = Color3.fromRGB(255, 0, 127)}):Play()
			print("Lights Are On")	
		end
	end
end)

The only way to automate tasks over groups of objects is using tables, so yes, you will need one. Personally right now I don’t think your script is too long. It’s actually a bit short. So good job. There’s nothing really wrong with this code, other than the fact that you check if the expression is true and do nothing, you can invert logical expressions, you know?

if expression then
else
    print("do something")
end

-- this is better
if not (expression) then
    print("do something")
end

How would doing invert logical expression work- Sorry I don’t understand. :sweat_smile:

you can use collection service and simply write a code like this

local CollectionService  = game:GetService("CollectionService")
local TweenService = game:GetService("TweenService")
local Info = TweenInfo.new(2, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut, -1, true, 0)

script.Parent.MouseButton1Click:Connect(function()
    for Light in pairs(CollectionService:GetTagged("your light tag here") do
        Light.Material = Enum.Material.Neon
        TweenService:Create(Light, Info, {Color = Color3.fromRGB(255, 0, 127)}):Play()
	print("Lights Are On")
    end
end)

this solution will void having to check for multiple different names as you can just add a tag describing the object as a light. if you want different lights to turn on for different buttons, make multiple tags for different light groups :smiley: (ex: LightGroup1, LightGroup2)
(if you dont have the tag plugin, there is a metadata object in the properties table shown below)
image

1 Like

ohhhhh! smart! I have never used tags before and didn’t think about it. I’ll try it soon and let you know how it goes, thak you!

The tags idea worked! This is the code I used based off what you gave me! Thank you!

local CollectionService  = game:GetService("CollectionService")
local TweenService = game:GetService("TweenService")
local Info = TweenInfo.new(2, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut, -1, true, 0)

script.Parent.MouseButton1Click:Connect(function()
	for i,v in pairs(CollectionService:GetTagged("A")) do
			v.Material = Enum.Material.Neon
			TweenService:Create(v, Info, {Color = Color3.fromRGB(255, 0, 127)}):Play()
		print("Lights Are On")
	end	
end)
1 Like

always happy to help! tags are a very useful feature and i hope you continue to use them to write efficient systems :smiley:

1 Like