Need help with :GetChildren script

I don’t know how to get the children of each part, its a bit hard to explain but I’ll just show you a screenshot.

So basically I’m trying to get all the point lights in the folder and I have no idea how. Maybe use a table or idk.
Screenshot 2023-07-04 120200

Here is the code:

local clickDetector = script.Parent.ClickDetector
local lights = game.Workspace.Lights["Ceiling Light"]:GetDescendants() --This line isnt usefull at all
local DEBOUNCE = false
local lightsOn = false

clickDetector.MouseClick:Connect(function()
	if not DEBOUNCE then
		DEBOUNCE = true
		
		if not lightsOn then
			lightsOn = true
			
			for i, v in pairs(lights) do
				v.Enabled = true
			end
			
		else
			
			for i, v in pairs(lights) do
				v.Enabled = false
			end
			
			lightsOn = false
		end
		
		DEBOUNCE = false
	end
end)

EDIT: I could just use a bunch of lines of code but I’d rather make the code efficient ya know?

1 Like

Try using GetDescendants() on the lights folder instead but check if it’s a PointLight too
Example:

local Lights = game:GetService("Workspace").Lights:GetDescendants()

local On = false

for i,v in next, Lights do
	On = not On
	if v:IsA("PointLight") then
		v.Enabled = On
	end
end
4 Likes

What does for i, v in “NEXT” do, do?

What does the next mean?

1 Like
3 Likes

It’s basically pairs with slightly different usage and name, you can use either though

1 Like

Instead of using :GetChildren() you can use :GetDescendents() which gets everything parented to the folder. Heres what you can do though.

for _, v in pairs(script.Parent.Parent:GetDescendents())
  if v:IsA("Pointlight") then
-- script
   end
end
1 Like

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