Simplification Light Scripts

Hello
I have a small problem with script with lights.
Script is ready and functional, but maybe…Simplification ???

--svetlo is word in czech language for light 
local Settings = script.Settings
local ProximityPrompts = script["Proximity Prompts"]

local svetlo01 = script.Parent.LampTube01.Svetlo
local svetlo02 = script.Parent.LampTube02.Svetlo
local svetlo03 = script.Parent.LampTube03.Svetlo
local svetlo04 = script.Parent.LampTube04.Svetlo
local svetlo05 = script.Parent.LampTube05.Svetlo
local svetlo06 = script.Parent.LampTube06.Svetlo
local svetlo07 = script.Parent.LampTube07.Svetlo
local svetlo08 = script.Parent.LampTube08.Svetlo
local svetlo09 = script.Parent.LampTube09.Svetlo
local svetlo10 = script.Parent.LampTube10.Svetlo
local svetlo11 = script.Parent.LampTube11.Svetlo
local svetlo12 = script.Parent.LampTube12.Svetlo
local svetlo13 = script.Parent.LampTube13.Svetlo
local svetlo14 = script.Parent.LampTube14.Svetlo
local svetlo15 = script.Parent.LampTube15.Svetlo
local svetlo16 = script.Parent.LampTube16.Svetlo
local svetlo17 = script.Parent.LampTube17.Svetlo
local svetlo18 = script.Parent.LampTube18.Svetlo





local proximityPrompts = {}
local connections = {}


local function getProximityPrompts()
	for index, child in pairs(ProximityPrompts:GetChildren()) do
		if child:IsA("ObjectValue") then
			local prompt = child.Value
			if prompt:IsA("ProximityPrompt") then
				table.insert(proximityPrompts, prompt)
			end
		end
	end
end

local function onTriggered(player)
	local model = script.Parent
	for index, descendant in pairs(model:GetDescendants()) do
		if descendant:IsA("Light") then
			if descendant.Enabled == true then
				descendant.Enabled = false
				svetlo01.Material =  Enum.Material.SmoothPlastic
				svetlo02.Material =  Enum.Material.SmoothPlastic
				svetlo03.Material =  Enum.Material.SmoothPlastic
				svetlo04.Material =  Enum.Material.SmoothPlastic
				svetlo05.Material =  Enum.Material.SmoothPlastic
				svetlo06.Material =  Enum.Material.SmoothPlastic
				svetlo07.Material =  Enum.Material.SmoothPlastic
				svetlo08.Material =  Enum.Material.SmoothPlastic
				svetlo09.Material =  Enum.Material.SmoothPlastic
				svetlo10.Material =  Enum.Material.SmoothPlastic
				svetlo11.Material =  Enum.Material.SmoothPlastic
				svetlo12.Material =  Enum.Material.SmoothPlastic
				svetlo13.Material =  Enum.Material.SmoothPlastic
				svetlo14.Material =  Enum.Material.SmoothPlastic
				svetlo15.Material =  Enum.Material.SmoothPlastic
				svetlo16.Material =  Enum.Material.SmoothPlastic
				svetlo17.Material =  Enum.Material.SmoothPlastic
				svetlo18.Material =  Enum.Material.SmoothPlastic



			else
				descendant.Enabled = true
				svetlo01.Material =  Enum.Material.Neon
				svetlo02.Material =  Enum.Material.Neon
				svetlo03.Material =  Enum.Material.Neon
				svetlo04.Material =  Enum.Material.Neon
				svetlo05.Material =  Enum.Material.Neon
				svetlo06.Material =  Enum.Material.Neon
				svetlo07.Material =  Enum.Material.Neon
				svetlo08.Material =  Enum.Material.Neon
				svetlo09.Material =  Enum.Material.Neon
				svetlo10.Material =  Enum.Material.Neon
				svetlo11.Material =  Enum.Material.Neon
				svetlo12.Material =  Enum.Material.Neon
				svetlo13.Material =  Enum.Material.Neon
				svetlo14.Material =  Enum.Material.Neon
				svetlo15.Material =  Enum.Material.Neon
				svetlo16.Material =  Enum.Material.Neon
				svetlo17.Material =  Enum.Material.Neon
				svetlo18.Material =  Enum.Material.Neon


			end
		end
	end
end


getProximityPrompts()
for index, prompt in ipairs(proximityPrompts) do
	local connection = prompt.Triggered:Connect(onTriggered)
	table.insert(connections, connection)
end

Thanks
Codycheck
script-lights

Why two script and two Trigger(switch)
This is system „stair switch“
On, Off Switch → Two place in map

You can use a for loop to simplify the script

Thanks for the response

I know LOOP with FOR
but LUA warning

local svetlo

for abc = 1,18 do

svetlo(abc) = script.Parent.LampTube(abc).Svetlo

end

svetlo(abc) is a function not a variable and you dont need variables with your situation

you should probably format it like this

local svetlo = {}; -- makes svetlo an array
for abc = 1,18 do --with abc starting at 1 and until it goes to 18
    svetlo[abc] = script.Parent:FindFirstChild("LampTube"..abc).Svetlo; --it will set an index in the array (1 - 18) to the appropriate lamptube. it joins LampTube with the number
end

the reason you can’t use LampTube(abc) is likely because roblox would get confused thinking you’re trying to use a function while using a pathway (i’m not that great at explaining this)

you’ll need to remove the starting 0 though in ur LampTube parts

1 Like

Try this:

--svetlo is word in czech language for light 
local Settings = script.Settings
local ProximityPrompts = script:WaitForChild("Proximity Prompts")

local proximityPrompts = {}
local connections = {}

local allSvetlo = {}

for i, child in ipairs(script.Parent:GetChildren()) do
	if not child.Name:match("LampTube") then
		continue
	end

	table.insert(allSvetlo, child.Svetlo)
end

local function getProximityPrompts()
	for index, child in pairs(ProximityPrompts:GetChildren()) do
		if child:IsA("ObjectValue") then
			local prompt = child.Value

			if prompt:IsA("ProximityPrompt") then
				table.insert(proximityPrompts, prompt)
			end
		end
	end
end

local function onTriggered(player)
	local model = script.Parent
	
	for index, descendant in pairs(model:GetDescendants()) do
		if descendant:IsA("Light") then
			descendant.Enabled = not descendant.Enabled
			
			if descendant.Enabled then
				for i, svetlo in ipairs(allSvetlo) do
					svetlo.Material = Enum.Material.Neon
				end
			else				
				for i, svetlo in ipairs(allSvetlo) do
					svetlo.Material = Enum.Material.SmoothPlastic
				end
			end
		end
	end
end


getProximityPrompts()

for index, prompt in ipairs(proximityPrompts) do
	local connection = prompt.Triggered:Connect(onTriggered)
	table.insert(connections, connection)
end

Thank you Katrist
Script is short and functional

But for understanding, I’ll leave it for the morning :slight_smile:
(I have 4 hours past midnight)

-- Author: Katrist September 2022

--svetlo is word in czech language for light

local Settings = script.Settings
local ProximityPrompts = script:WaitForChild("Proximity Prompts")

local proximityPrompts = {}
local connections = {}

local allSvetlo = {}

for i, child in ipairs(script.Parent:GetChildren()) do
	if not child.Name:match("LampTube") then
		continue
	end

	table.insert(allSvetlo, child.Svetlo)
end

local function getProximityPrompts()
	for index, child in pairs(ProximityPrompts:GetChildren()) do
		if child:IsA("ObjectValue") then
			local prompt = child.Value

			if prompt:IsA("ProximityPrompt") then
				table.insert(proximityPrompts, prompt)
			end
		end
	end
end

local function onTriggered(player)
	local model = script.Parent

	for index, descendant in pairs(model:GetDescendants()) do
		if descendant:IsA("Light") then
			descendant.Enabled = not descendant.Enabled

			if descendant.Enabled then
				for i, svetlo in ipairs(allSvetlo) do
					svetlo.Material = Enum.Material.Neon
				end
			else				
				for i, svetlo in ipairs(allSvetlo) do
					svetlo.Material = Enum.Material.SmoothPlastic
				end
			end
		end
	end
end


getProximityPrompts()

for index, prompt in ipairs(proximityPrompts) do
	local connection = prompt.Triggered:Connect(onTriggered)
	table.insert(connections, connection)
end
1 Like

shortening the script also works
Thank you

create a folder and put the lampTubes in the folder. then do

for i,v in pairs(folder:GetChildren()) do 
v.Material =  Enum.Material.SmoothPlastic
end

the folder wont change any physical properties of the parts

Screen Video
Thanks for All

PS: I am constantly confused with the logic of the Roblox script, since I started 35 years ago with the BASIC language :slight_smile:

No problem.

Goodluck on your game and learning luau!