How to call all parts in a folder (light model + switch)

Hi,

I made a lightswitch that’s supposed to turn on/off all the wall lights in the main hall. I put all the models in one folder to call them, but sadly it doesn’t work.

Could someone explain to me what I’m doing wrong?


image

What you are doing is simply getting a single part, that well doesnt exist for that is not how you loop through parts and get the stuff in them.
What I’d recommend is looking into looping through objects, there are some tutorials out there that can teach you better than I can

1 Like

It’s impossible to call parts.

What you can do is loop through the folder and set the light enabled property for each light.

1 Like
local ProximityPrompt = script.Parent
local Bulb = game.Workspace:WaitForChild("Main Hall Lights"):WaitForChild("Bulb")
local Spotlight = Bulb.Spotlight

ProximityPrompt.Triggered:Connect(function()
   for _, spotlight in ipairs(workspace["Main Hall Lights"]:GetDescendants()) do
      if spotlight:IsA("SpotLight") then
         spotlight.Enabled = not spotlight.Enabled
      end
   end
end)

@Nabilekes
Koxf

This can help you manage better

1 Like

So it’s the descendants you used? I’ll try and remember that. I tried your code, but sadly doesn’t work.

I managed to fix it :slight_smile:

Updated code:

local ProximityPrompt = script.Parent
local LightsFolder = game.Workspace:WaitForChild("Main Hall Lights")

ProximityPrompt.Triggered:Connect(function(player)
    for _, child in ipairs(LightsFolder:GetChildren()) do
        local bulb = child:FindFirstChild("Bulb")
        if bulb then
            local spotlight = bulb:FindFirstChild("SpotLight")
            if spotlight and spotlight:IsA("SpotLight") then
                spotlight.Enabled = not spotlight.Enabled
            end
        end
    end
end)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.