Hello. I'm trying to make a script that while touched, turns the color of all the lights in a game red, and the point lights inside them red as well. I tried to use "GetDescendants" and "GetChildren", but they didn't seem to work. The light parts are all in a model called "lights" in Workspace. Is there even a correct way to change the properties of all parts in a model at once without having to address them all individually? Any help would be greatly appreciated. Thanks!
local lights = game.Workspace.lights:GetDescendants() local part = script.Parent local pointlights = lights:GetChildren():GetChildren("PointLight") part.Touched:connect(function() lights.BrickColor = BrickColor.new("Bright red") pointlights.Color = Color3.new(1, 0.109804, 0.109804) end) script.Parent.TouchEnded:connect(function() lights.BrickColor = BrickColor.new("Institutional white") pointlights.Color = Color3.new(1, 1, 1) end)
using a for loop, you can loop through all the descendants or children of an object
local lights = game.Workspace.lights local part = script.Parent part.Touched:connect(function() for i, v in pairs(lights:GetDescendants()) do if v:IsA("PointLight") then v.Parent.BrickColor = BrickColor.new("Bright red") v.Color = Color3.fromRGB(COLOR) -- this color is using RGB so copy and paste the color you want for it end end end) script.Parent.TouchEnded:connect(function() for i, v in pairs(lights:GetDescendants()) do if v:IsA("PointLight") then v.Parent.BrickColor = BrickColor.new("Institutional white") v.Color = Color3.fromRGB(COLOR) -- this color is using RGB so copy and paste the color you want for it end end end)
Yes, there should be a proper way to do this, and by golly please do not manually setting all those light properties. Here is how I would personally set up code to follow
part.Touched:connect(function() for i, v in pairs(lights:GetChildren()) do v.BrickColor = BrickColor.new("Bright red") for index, value in pairs(v:GetChildren()) do value.Color = Color3.new(1,0.109804, .109804) end end end) part.TouchedEnded:connect(function() for i, v in pairs(lights:GetChildren()) do v.BrickColor = BrickColor.new("Institutional white") for index, value in pairs(v:GetChildren()) do value.Color = Color3.new(1,1,1) end end end)
I believe you will come across an issue that I should address. Touched events and especially TouchEnded typically do not work how you want them. When a part stops moving on top of a another part, TouchedEnded fires. Doesn't matter if it "actually left" or not, this issue still occurs. I recommend looking up solutions to around this. Here are some resources that you may want to visit. Resource 1; Resource 2