Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I make it so that if a certain condition is met, all properties of a whole model will change?

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?

3 comments

0

Definitely! I'll work on an answer.

0

Answered!

0

Please accept my answer if it helped!

1 answer

✓ Accepted answer

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

1 comment

0

Thank you for answering and helping me understand how to make my code cleaner! The script looks more organized than before.

This discussion is preserved as it was. New answers, comments, and votes are closed.