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:

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

Script that works (for one part):

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

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:

01local ReplicatedStorage = game:GetService("ReplicatedStorage")
02local ExtLightsEvent = ReplicatedStorage:WaitForChild("ExtLightsEvent")
03 
04ExtLightsEvent.OnServerEvent:Connect(function(player, ExtLight)
05    for _, v in pairs(ExtLight:GetChildren()) do
06        -- v acts as the element in the table
07        if v:IsA("BasePart") and v.Transparency <= 0 then
08            -- prone to script error if the object isn't a part. i got you
09            v.Transparency = 1
10        end
11    end
12end)

"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