Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to change the properties of all parts in a model at once?

Asked by 4 years ago

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)

2 answers

Log in to vote
1
Answered by 4 years ago

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)
0
While yes, this does answer his question, I personally believe he has a more underlying problem. That being TouchEnded being so finicky and so useless. I swear I had this typed before you sent your comment on my post lol* beeswithstingerss 41 — 4y
0
I swear I didn’t copy yours, ours are different Frostbyte3337372 42 — 4y
Ad
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

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

0
i wouldn't use "TouchEnded" because it fires when a player is still on an object, ray casting would be more efficient Frostbyte3337372 42 — 4y
0
Raycasting would be semi-effective. I can imagine a couple scenarios when that wouldn't do the trick. Let's say we have an invis and non-collidable part between the player and the floor. This will mess up the ray cast. I would recommend for more advanced scripters to use Heartbeat and a GetTouchingParts/Region3 to keep a list of players touching. beeswithstingerss 41 — 4y

Answer this question