[SCRIPT HELP] When all lamps turn off

I have a couple of lamp in Workspace and inside the lamp there was a PointLight. If all the lamp’s PointLight is off then something happend. What is the script for that? I already try ChatGPT but it sucks.

1 Like

You can store all the PointLight(s) in an array as such:

local pointLights: any = {
    [1] = POINT_LIGHT_PATH,
    [2] = POINT_LIGHT_PATH,
}

Then you can use a :GetPropertyChangedSignal() signal for each light to check if all the lights are turned off:

local function CheckAllLights()
    for index: number, pointLight: PointLight in pairs(pointLights :: any) do
        if (pointLight.Enabled) then
            return false
        end
    end

    return true
end

for index: number, pointLight: PointLight in pairs(pointLights :: any) do
    pointLight:GetPropertyChangedSignal("Enabled"):Connect(function()
        if (CheckAllLights()) then
            -- do what you want to do when all the lights are turned off
        end
    end
end

Better yet you can use CollectionService to tag the PointLight(s). So after you give the tag to the PointLight(s) you can replace the array with:

-- services
local collectionService: CollectionService = game:GetService("CollectionService")

-- variables
local pointLights: any = collectionService:GetTagged("Light") -- 'light' can be whatever your tag name is.

Hope this helps!

2 Likes

workspace.lamp or workspace.lamp.PointLight for this?

workspace.lamp.PointLight for those. You’ll need to have each object have a unique name though (even something like workspace.lamp1.Pointlight; workspace.lamp2.Pointlight etc.

Is there any way to do that without unique names?

if you put them all into a model or folder (Have the name be “Lamps”, for example), then do this:

local pointLights = {}

for _, light in ipairs(game.Workspace.Lamps:GetChildren()) do
    table.insert(pointLights, light.PointLight)
end

If you don’t want to have to put them all in one model, let me know because there is one other method that would be viable.


(Also Inpultion, using typechecking for this is just going to complicate things and confuse OP)

:rofl: kinda true. Well, its awesome for certain tasks, just dont ask it to script your game

Doesnt need to have unique names.

Doesnt need to be all in the same folder, but kinda helps, just look for IsA():

for _, light in pairs(game.Workspace.Lamps:GetDescendants()) do
	if light:IsA("PointLight") then
		table.insert(pointLights, light)
		light:GetPropertyChangedSignal("Enabled"):Connect(function()
			if CheckAllLights() then

			end
		end)
	end
end

Might be better if he stored all the lights in a folder, would be quite the hassle to type out every single one instead of doing :GetChildren()

Collection service is a better idea for this

(Never mind, You posted the same thing before I commented)

Yeah, my second response suggests putting them in a folder and getting them that way. Collection service would probably also work (I just haven’t used collection service ever so I can’t really comment on it)

UnknownGlobal: (31,16) Unknown global ‘pointLights’
UnknownGlobal: (33,7) Unknown global ‘CheckAllLights’

???
Emm, you gotta define the CheckAllLights() function…
Same for pointLights table…

Is this okay?

local pointLights = {}

for _, light in ipairs(game.Workspace.lamp:GetChildren()) do
	table.insert(pointLights, light.SurfaceLight)
end
-- variables
local collectionService: CollectionService = game:GetService("CollectionService")
local pointLights: any = collectionService:GetTagged("SpotLight") -- 'light' can be whatever your tag name is.
-- do something here
local function CheckAllLights()
    for index: number, pointLight: PointLight in pairs(pointLights :: any) do
        if (pointLight.Enabled) then
            return false
        end
    end

    return true
end

for _, light in pairs(game.Workspace.Lamps:GetDescendants()) do
	if light:IsA("SpotLight") then --- Btw I forgot that it was a SpotLight
		table.insert(pointLights, light)
		light:GetPropertyChangedSignal("Enabled"):Connect(function()
			if CheckAllLights() then

			
		----Something

			end
		end)
	end
end

Not really, you are merging the different approaches me and other devs suggested. Like you are populating the pointLights table with the approach @SeargentAUS suggested, and at the bottom, overwritting the same table with my approach, and using different paths for it. game.Workspace.lamp or game.Workspace.Lamps ?

Then, you gonna use the CollectionService? or not? your pointlights are already tagged to be collected?

oops my apologize

no, but if i delete it an error appear

To me, would be enough to do this:

local pointLights = {}

local function CheckAllLights()
	for _, pointLight in pairs(pointLights) do
		if pointLight.Enabled then
			return false
		end
	end
	return true
end

for _, light in pairs(game.Workspace.Lamps:GetDescendants()) do
	if light:IsA("SpotLight") then --- Btw I forgot that it was a SpotLight
		table.insert(pointLights, light)
		light:GetPropertyChangedSignal("Enabled"):Connect(function()
			if CheckAllLights() then
				warn("All lights are off")
			end
		end)
	end
end

Just make sure all your lamps are in this folder: game.Workspace.Lamps
I dont mean only the SpotLights, could be the entire lamp model, doesnt matter

Still not work
image

I use your script after you edit it

How are your turning the lights off? client or server side? and this script is client or server side?

It’s server side. And using regular script

I just tested the code I sent, and works as intended without errors and showing a warn when all lights are off.
You sure its not related with client vs server?

And I meant, both scripts. The script that turns the lights off, where is located? is client or server sided. And this script we’re working on is server or client?

Yes I’m sure is not because of client vs server. Both script and the lamp location are Server