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

Use GetChildren() or script one by one?

Asked by
snewo12 72
6 years ago

So I want to change the property of all these parts by a RemoteEvent (see picture). But how do I do that? Do I just script them one by one or is there a faster way? Something with GetChildren()? (Tag me when a solution is found)

Picture: http://prntscr.com/k8rl2t

Script that I tried:

game.ReplicatedStorage:WaitForChild("ExteriorLightsEvent").OnServerEvent:Connect(function(Player, ExtLight)
    if ExtLight:GetChildren().Transparency == 0 then
    ExtLight:GetChildren().Transparency = 1
else
    ExtLight:GetChildren().Transparency = 0
    end
end)

Script that works (for one part):

game.ReplicatedStorage:WaitForChild("ExteriorLightsEvent").OnServerEvent:Connect(function(Player, ExtLight)
    if ExtLight.hlight2.Transparency == 0 then
    ExtLight.hlight2.Transparency = 1
else
    ExtLight.hlight2.Transparency = 0
    end
end)

Thanks in advance.

1 answer

Log in to vote
1
Answered by 6 years ago
Edited 6 years ago

You can't use GetChildren like that. You're attempting to get every single object from the table, but incorrectly. You can use a for loop to accomplish this:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ExtLightsEvent = ReplicatedStorage:WaitForChild("ExtLightsEvent")

ExtLightsEvent.OnServerEvent:Connect(function(player, ExtLight)
    for _, v in pairs(ExtLight:GetChildren()) do
        -- v acts as the element in the table
        if v:IsA("BasePart") and v.Transparency <= 0 then
            -- prone to script error if the object isn't a part. i got you
            v.Transparency = 1
        end
    end
end)

"Use GetChildren() or script one by one?"

Use GetChildren. Scripting one by one is time consuming, complicated at times, and life could be made easier from one script that handles everything.

1
Ah thank you! snewo12 72 — 6y
Ad

Answer this question