SecurityStationFrontAlarm = game.Workspace.Alarms.SecurityRoomFrontAlarm SecurityStationLeftAlarm = game.Workspace.Alarms.SecurityRoomLeftAlarm SecurityStationRightAlarm = game.Workspace.Alarms.SecurityRoomRightAlarm CellRoomAlarm = game.Workspace.Alarms.CellsAlarm script.Parent.GreenButton.ClickDetector.MouseClick:connect(function() --GREEN PRESSED SecurityStationFrontAlarm.glass1.BrickColor = BrickColor.new("White") SecurityStationFrontAlarm.glass2.BrickColor = BrickColor.new("White") SecurityStationFrontAlarm.light1.SpotLight.Range = 0 SecurityStationFrontAlarm.light2.SpotLight.Range = 0 SecurityStationFrontAlarm.light1.BrickColor = BrickColor.new("White") SecurityStationFrontAlarm.light2.BrickColor = BrickColor.new("White") SecurityStationFrontAlarm.light1.SpotLight.Color = Color3.new(0, 0, 0) SecurityStationFrontAlarm.light2.SpotLight.Color = Color3.new(0, 0, 0) SecurityStationLeftAlarm.glass1.BrickColor = BrickColor.new("White") SecurityStationLeftAlarm.glass2.BrickColor = BrickColor.new("White") SecurityStationLeftAlarm.light1.SpotLight.Range = 0 SecurityStationLeftAlarm.light2.SpotLight.Range = 0 SecurityStationLeftAlarm.light1.BrickColor = BrickColor.new("White") SecurityStationLeftAlarm.light2.BrickColor = BrickColor.new("White") SecurityStationLeftAlarm.light1.SpotLight.Color = Color3.new(0, 0, 0) SecurityStationLeftAlarm.light2.SpotLight.Color = Color3.new(0, 0, 0) SecurityStationRightAlarm.glass1.BrickColor = BrickColor.new("White") SecurityStationRightAlarm.glass2.BrickColor = BrickColor.new("White") SecurityStationRightAlarm.light1.SpotLight.Range = 0 SecurityStationRightAlarm.light2.SpotLight.Range = 0 SecurityStationRightAlarm.light1.BrickColor = BrickColor.new("White") SecurityStationRightAlarm.light2.BrickColor = BrickColor.new("White") SecurityStationRightAlarm.light1.SpotLight.Color = Color3.new(0, 0, 0) SecurityStationRightAlarm.light2.SpotLight.Color = Color3.new(0, 0, 0) CellRoomAlarm.glass1.BrickColor = BrickColor.new("White") CellRoomAlarm.glass2.BrickColor = BrickColor.new("White") CellRoomAlarm.light1.SpotLight.Range = 0 CellRoomAlarm.light2.SpotLight.Range = 0 CellRoomAlarm.light1.BrickColor = BrickColor.new("White") CellRoomAlarm.light2.BrickColor = BrickColor.new("White") CellRoomAlarm.light1.SpotLight.Color = Color3.new(0, 0, 0) CellRoomAlarm.light2.SpotLight.Color = Color3.new(0, 0, 0) SecurityStationFrontAlarm.RedAlarm:Stop() SecurityStationFrontAlarm.YellowAlarm:Stop() end) script.Parent.YellowButton.ClickDetector.MouseClick:connect(function() --YELLOW PRESSED SecurityStationFrontAlarm.glass1.BrickColor = BrickColor.new("Bright Yellow") SecurityStationFrontAlarm.glass2.BrickColor = BrickColor.new("Bright Yellow") SecurityStationFrontAlarm.light1.SpotLight.Range = 40 SecurityStationFrontAlarm.light2.SpotLight.Range = 40 SecurityStationFrontAlarm.light1.BrickColor = BrickColor.new("Bright Yellow") SecurityStationFrontAlarm.light2.BrickColor = BrickColor.new("Bright Yellow") SecurityStationFrontAlarm.light1.SpotLight.Color = Color3.new(255, 255, 0) SecurityStationFrontAlarm.light2.SpotLight.Color = Color3.new(255, 255, 0) SecurityStationLeftAlarm.glass1.BrickColor = BrickColor.new("Bright Yellow") SecurityStationLeftAlarm.glass2.BrickColor = BrickColor.new("Bright Yellow") SecurityStationLeftAlarm.light1.SpotLight.Range = 40 SecurityStationLeftAlarm.light2.SpotLight.Range = 40 SecurityStationLeftAlarm.light1.BrickColor = BrickColor.new("Bright Yellow") SecurityStationLeftAlarm.light2.BrickColor = BrickColor.new("Bright Yellow") SecurityStationLeftAlarm.light1.SpotLight.Color = Color3.new(255, 255, 0) SecurityStationLeftAlarm.light2.SpotLight.Color = Color3.new(255, 255, 0) SecurityStationRightAlarm.glass1.BrickColor = BrickColor.new("Bright Yellow") SecurityStationRightAlarm.glass2.BrickColor = BrickColor.new("Bright Yellow") SecurityStationRightAlarm.light1.SpotLight.Range = 40 SecurityStationRightAlarm.light2.SpotLight.Range = 40 SecurityStationRightAlarm.light1.BrickColor = BrickColor.new("Bright Yellow") SecurityStationRightAlarm.light2.BrickColor = BrickColor.new("Bright Yellow") SecurityStationRightAlarm.light1.SpotLight.Color = Color3.new(255, 255, 0) SecurityStationRightAlarm.light2.SpotLight.Color = Color3.new(255, 255, 0) CellRoomAlarm.glass1.BrickColor = BrickColor.new("Bright Yellow") CellRoomAlarm.glass2.BrickColor = BrickColor.new("Bright Yellow") CellRoomAlarm.light1.SpotLight.Range = 40 CellRoomAlarm.light2.SpotLight.Range = 40 CellRoomAlarm.light1.BrickColor = BrickColor.new("Bright Yellow") CellRoomAlarm.light2.BrickColor = BrickColor.new("Bright Yellow") CellRoomAlarm.light1.SpotLight.Color = Color3.new(255, 255, 0) CellRoomAlarm.light2.SpotLight.Color = Color3.new(255, 255, 0) SecurityStationFrontAlarm.RedAlarm:Stop() SecurityStationFrontAlarm.YellowAlarm:Play() end) script.Parent.RedButton.ClickDetector.MouseClick:connect(function() --RED PRESSED SecurityStationFrontAlarm.glass1.BrickColor = BrickColor.new("Bright Red") SecurityStationFrontAlarm.glass2.BrickColor = BrickColor.new("Bright Red") SecurityStationFrontAlarm.light1.SpotLight.Range = 40 SecurityStationFrontAlarm.light2.SpotLight.Range = 40 SecurityStationFrontAlarm.light1.BrickColor = BrickColor.new("Bright Red") SecurityStationFrontAlarm.light2.BrickColor = BrickColor.new("Bright Red") SecurityStationFrontAlarm.light1.SpotLight.Color = Color3.new(255, 0, 0) SecurityStationFrontAlarm.light2.SpotLight.Color = Color3.new(255, 0, 0) SecurityStationLeftAlarm.glass1.BrickColor = BrickColor.new("Bright Red") SecurityStationLeftAlarm.glass2.BrickColor = BrickColor.new("Bright Red") SecurityStationLeftAlarm.light1.SpotLight.Range = 40 SecurityStationLeftAlarm.light2.SpotLight.Range = 40 SecurityStationLeftAlarm.light1.BrickColor = BrickColor.new("Bright Red") SecurityStationLeftAlarm.light2.BrickColor = BrickColor.new("Bright Red") SecurityStationLeftAlarm.light1.SpotLight.Color = Color3.new(255, 0, 0) SecurityStationLeftAlarm.light2.SpotLight.Color = Color3.new(255, 0, 0) SecurityStationRightAlarm.glass1.BrickColor = BrickColor.new("Bright Red") SecurityStationRightAlarm.glass2.BrickColor = BrickColor.new("Bright Red") SecurityStationRightAlarm.light1.SpotLight.Range = 40 SecurityStationRightAlarm.light2.SpotLight.Range = 40 SecurityStationRightAlarm.light1.BrickColor = BrickColor.new("Bright Red") SecurityStationRightAlarm.light2.BrickColor = BrickColor.new("Bright Red") SecurityStationRightAlarm.light1.SpotLight.Color = Color3.new(255, 0, 0) SecurityStationRightAlarm.light2.SpotLight.Color = Color3.new(255, 0, 0) CellRoomAlarm.glass1.BrickColor = BrickColor.new("Bright Red") CellRoomAlarm.glass2.BrickColor = BrickColor.new("Bright Red") CellRoomAlarm.light1.SpotLight.Range = 40 CellRoomAlarm.light2.SpotLight.Range = 40 CellRoomAlarm.light1.BrickColor = BrickColor.new("Bright Red") CellRoomAlarm.light2.BrickColor = BrickColor.new("Bright Red") CellRoomAlarm.light1.SpotLight.Color = Color3.new(255, 0, 0) CellRoomAlarm.light2.SpotLight.Color = Color3.new(255, 0, 0) SecurityStationFrontAlarm.RedAlarm:Play() SecurityStationFrontAlarm.YellowAlarm:Play() end)
The script controls 3 buttons, which when pressed change the alarm systems state (Green, Yellow and Red). I need help simplifying this, so it searches alarms for it's children and modifies them instead of me manually defining what to modify. Help!
There is probably a way to simplify this down further but this is still half the length of code you have.
Breakdown
I saw that you were setting only two variables (color and range) so created a function that took both of these variables as arguments.
local function SetAlarm(lightColor, lightRange) end
Next, upon inspection I found that all of the objects you were changing were descendants of one of the four alarm objects. So I set up a table of the alarm objects and looped through them. For each alarm object I would need to examine it's children, so I created a loop for those as well.
local function SetAlarm(lightColor, lightRange) local Alarms = { ["Front"] = game.Workspace.Alarms.SecurityRoomFrontAlarm, ["Left"] = game.Workspace.Alarms.SecurityRoomLeftAlarm, ["Right"] = game.Workspace.Alarms.SecurityRoomRightAlarm, ["Cells"] = game.Workspace.Alarms.CellsAlarm } for _, alarm in pairs (Alarms) do for _, object in ipairs (alarm:GetChildren()) do end end end
Next, I noticed that all of the children had a class which was a member of BasePart, so I used an if statement to only look at those children. From there I changed their BrickColor. But I also had to check that child's children to see if It had a spotlight as a descendant. I used a loop and if one was found, I would change the light's range and color.
local function SetAlarm(lightColor, lightRange) local Alarms = { ["Front"] = game.Workspace.Alarms.SecurityRoomFrontAlarm, ["Left"] = game.Workspace.Alarms.SecurityRoomLeftAlarm, ["Right"] = game.Workspace.Alarms.SecurityRoomRightAlarm, ["Cells"] = game.Workspace.Alarms.CellsAlarm } for _, alarm in pairs (Alarms) do for _, object in ipairs (alarm:GetChildren()) do if object:IsA("BasePart") then object.BrickColor = BrickColor.new(lightColor) for _, child in ipairs(object:GetChildren()) do if child:IsA("SpotLight") then child.Range = lightRange child.Color = lightColor end end end end end end
Lastly, I had to set the sounds. To do this I created two more arguments that I would set to either true or false, and play or pause the sounds accordingly.
local function SetAlarm(lightColor, lightRange, RedAlarmSound, YellowAlarmSound) local Alarms = { ["Front"] = game.Workspace.Alarms.SecurityRoomFrontAlarm, ["Left"] = game.Workspace.Alarms.SecurityRoomLeftAlarm, ["Right"] = game.Workspace.Alarms.SecurityRoomRightAlarm, ["Cells"] = game.Workspace.Alarms.CellsAlarm } for _, alarm in pairs (Alarms) do for _, object in ipairs (alarm:GetChildren()) do if object:IsA("BasePart") then object.BrickColor = BrickColor.new(lightColor) for _, child in ipairs(object:GetChildren()) do if child:IsA("SpotLight") then child.Range = lightRange child.Color = lightColor end end end end end if RedAlarmSound then Alarms["Front"].RedAlarm:Play() else Alarms["Front"].RedAlarm:Stop() end if YellowAlarmSound then Alarms["Front"].YellowAlarm:Play() else Alarms["Front"].YellowAlarm:Stop() end end
Now that the function that changes the properties is complete, all we have to do is set up the events. To do this, I created two arrays. One would hold the different buttons and the other held the properties. I used a loop to create the three MouseClick events.
local buttons = { ["Red"] = script.Parent.RedButton, ["Yellow"] = script.Parent.YellowButton, ["Green"] = script.Parent.GreenButton } local arguments = { ["Red"] = {Color3.new(1, 0, 0), 40, true, true}, ["Yellow"] = {Color3.new(1, 1, 0), 40, false, true}, ["Green"] = {Color3.new(1, 1, 1), 0, false, false} } for color, button in pairs (buttons) do button.ClickDetector.MouseClick:connect(function() SetAlarm(arguments[color][1], arguments[color][2], arguments[color][3], arguments[color][4]) end) end
Full Script
local function SetAlarm(lightColor, lightRange, RedAlarmSound, YellowAlarmSound) local Alarms = { ["Front"] = game.Workspace.Alarms.SecurityRoomFrontAlarm, ["Left"] = game.Workspace.Alarms.SecurityRoomLeftAlarm, ["Right"] = game.Workspace.Alarms.SecurityRoomRightAlarm, ["Cells"] = game.Workspace.Alarms.CellsAlarm } for _, alarm in pairs (Alarms) do for _, object in ipairs (alarm:GetChildren()) do if object:IsA("BasePart") then object.BrickColor = BrickColor.new(lightColor) for _, child in ipairs(object:GetChildren()) do if child:IsA("SpotLight") then child.Range = lightRange child.Color = lightColor end end end end end if RedAlarmSound then Alarms["Front"].RedAlarm:Play() else Alarms["Front"].RedAlarm:Stop() end if YellowAlarmSound then Alarms["Front"].YellowAlarm:Play() else Alarms["Front"].YellowAlarm:Stop() end end local buttons = { ["Red"] = script.Parent.RedButton, ["Yellow"] = script.Parent.YellowButton, ["Green"] = script.Parent.GreenButton } local arguments = { ["Red"] = {Color3.new(1, 0, 0), 40, true, true}, ["Yellow"] = {Color3.new(1, 1, 0), 40, false, true}, ["Green"] = {Color3.new(1, 1, 1), 0, false, false} } for color, button in pairs (buttons) do button.ClickDetector.MouseClick:connect(function() SetAlarm(arguments[color][1], arguments[color][2], arguments[color][3], arguments[color][4]) end) end