Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Use GetChildren() or script one by one?

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

✓ Accepted answer

###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 comment

1

Ah thank you!

This discussion is preserved as it was. New answers, comments, and votes are closed.