How could I search for the Value IsOn in all folders available

How could I search for the Value IsOn in all folders available.

image

Basically to elaborate what im attempting to do:
Have a main breaker switch which turns off all the lights in the building, so search each lighting ‘zone’ or folder for the IsOn value, and then if it finds it, set it to 0 (it’s a 1 and 0 system as it has multiple states than 1 or 0)

Use a for loop and GetChildren to iterate the children of the “Lighting” folder, check if the child is a folder with IsA (or ClassName), use FindFirstChild on the child folder to check if it has a child named “IsOn”, if it does then set the value

1 Like

I’ll give this a go and update further

Hello.

You could just create a table with all the values in the folder, then you can loop through them, and get their values.

An Example

local folder = * -- the folder we are searching from
local switches = {} -- the list every boolen value in the folder is stored in

for index, object in pairs(folder:GetDescendants()) do

    if object and object:IsA("BoolValue") then

        table.insert(switches, object) -- add every boolen value to the list

    end

end
1 Like

The problem is here that another script checks the IsOn values separately in zones and I don’t want to redo this at the moment.

Thanks for your input

1 Like

Hi,

I’ve tried the following bit of code:

function on()
	for _, child in pairs(script.Parent:GetChildren()) do
		if child:IsA("Folder") then
			local zones = child:FindFirstChild("IsOn")
			zones.IsOn.Value = 1
		end
	end
end

However this results in the error:
IsOn is not a valid member of Folder "Workspace.BuildingSystems.Lighting"

Could you please advise on what to do next.

Thanks

Check if it’s exist.

function on()
	for _, child in pairs(script.Parent:GetChildren()) do
		if child:IsA("Folder") then
			local zones = child:FindFirstChild("IsOn")
			-- // Check if "IsOn" exist.
			if zones then
				zones.Value = 1
			end
		end
	end
end

(Edited)

1 Like

I still get the error

IsOn is not a valid member of Folder “Workspace.BuildingSystems.Lighting”

function on()
	for _, child in pairs(script.Parent:GetChildren()) do
		if child:IsA("Folder") then
			local zones = child:FindFirstChild("IsOn")
			if zones then
				zones.IsOn.Value = 1
			end
		end
	end
end

I fixed that, sorry. (Scroll up and copy the code again) :frowning_face:

1 Like

Ah, I didn’t notice, sorry.


This time it does nothing but also does not give an error.

Any ideas?

image

Here is what I am working with.
The script highlighted is the script which contains what I am trying to do.

Uhh… try this?

function on()
	for _, child in pairs(script.Parent.Parent:GetChildren()) do
		if child:IsA("Folder") then
			local zones = child:FindFirstChild("IsOn")
			if zones then
				zones.Value = 1
			end
		end
	end
end
1 Like

My script now works perfectly. Thanks for your support. The following is working.
image

1 Like