Hi! I am making a building with twenty windows. I want the windows to change its material to neon if it is nighttime. I don't want to change each property of the windows one by one, as the script would be so long and messy.
The script below is my current script:
-- Defining the variables. local day = game.Workspace.Buttons.Day local night = game.Workspace.Buttons.Night local windows = game.Workspace.Windows -- Windows is the name of my model containing the 20 parts. -- Script for day and night. day.Touched:Connect(function(hit) if game.Players:GetPlayerFromCharacter(hit.Parent) then game.Lighting.TimeOfDay = "00:00:00" end end) night.Touched:Connect(function(hit) if game.Players:GetPlayerFromCharacter(hit.Parent) then game.Lighting.TimeOfDay = "14:00:00" end end) -- This is the script for the windows. It works fine but it is so long and repetitive. People say it's bad practice. if game.Lighting == "00:00:00" then game.Windows.FirstPart.Material = Enum.Material.Neon game.Windows.SecondPart.Material = Enum.Material.Neon game.Windows.ThirdPart.Material = Enum.Material.Neon game.Windows.FourthPart.Material = Enum.Material.Neon game.Windows.FifthPart.Material = Enum.Material.Neon -- 15 more Enum.Material.Neon lines. elseif game.Lighting == "14:00:00" then game.Windows.FirstPart.Material = Enum.Material.SmoothPlastic game.Windows.SecondPart.Material = Enum.Material.SmoothPlastic game.Windows.ThirdPart.Material = Enum.Material.SmoothPlastic game.Windows.FourthPart.Material = Enum.Material.SmoothPlastic game.Windows.FifthPart.Material = Enum.Material.SmoothPlastic -- 15 more Enum.Material.SmoothPlastic lines. end
The script works as it should but is there any way to shorten this?
Ok, first of all, let's clean up this whole Enum.Material
stuff, so let's just make a variable for this:
local SmoothPlastic = Enum.Material.SmoothPlastic local Neon = Enum.Material.Neon
Next you will want to clean up the whole lines of game.Windows.ThirdPart.Material = Enum.Material.SmoothPlastic
, for this we could use a for
loop, like this:
-- New variables local SmoothPlastic = Enum.Material.SmoothPlastic local Neon = Enum.Material.Neon local Windows = game.Windows local TurnedTo = Windows:GetChildren() -- New code for _,v in ipairs(TurnedTo) do v.Material = Neon -- e.g. end
But, you may have children that are not parts, so we fix it like this:
for _,v in ipairs(TurnedTo) do if v:IsA("Part") then v.Material = Neon end end
Complete code:
local day = game.Workspace.Buttons.Day local night = game.Workspace.Buttons.Night local windows = game.Workspace.Windows -- Windows is the name of my model containing the 20 parts. local SmoothPlastic = Enum.Material.SmoothPlastic local Neon = Enum.Material.Neon local Windows = game.Windows local TurnedTo = Windows:GetChildren() day.Touched:Connect(function(hit) if game.Players:GetPlayerFromCharacter(hit.Parent) then game.Lighting.TimeOfDay = "00:00:00" end end) night.Touched:Connect(function(hit) if game.Players:GetPlayerFromCharacter(hit.Parent) then game.Lighting.TimeOfDay = "14:00:00" end end) if game.Lighting.TimeOfDay == "00:00:00" then for _,v in ipairs(TurnedTo) do if v:IsA("Part") then v.Material = Neon end end elseif game.Lighting == "14:00:00" then for _,v in ipairs(TurnedTo) do if v:IsA("Part") then v.Material = SmoothPlastic end end end