[SCRIPT HELP] When all lamps turn off

Can you show how you are handling the lights off then? the script

I’m using math.random() to turn off the lamps. The script is on each lamps

You gotta be more descriptive or show the script. You mean, a player idk clicks a button, and randomly you choose a lamp to turn off? The warning will show only when all lights are disabled

Every random seconds one of the lights will turn off automatically. And at some possibilities all the lights will turn off at the same times and at that point something happens.

And how you are disabling the light by changing what into the SpotLight? The script consider the property Enabled to run, not any other like bringing down the Brightness

Testing the script vid:

I’m turning off the light using Enabled = false.

so you’re trying to make one of the lights randomly turn off every random duration, and there would be a chance for all the lights to turn off at the same time then something would happen, right?

if i’m right, i’ll be writing a script for you in a moment…

Check the video I added into my previous message, Im testing the script and it works normally.

Add a print in the changed signal event to debug:

light:GetPropertyChangedSignal("Enabled"):Connect(function()
       warn("Got Signal")			
       if CheckAllLights() then

I think thats what @bagussetya11 wants!
Thats the importance to be descriptive in the initial post or at least show some code… None of the code me and others suggested is intended to work like that :roll_eyes: :face_with_hand_over_mouth:

Yeah maybe code before these lines could be wrong too :v
Would be better to forget this approach. I mean, or build a new one or show exactly how you are deciding lights turn off or not, you can even call the “thing to happen when lights goes off” from the script that randomly enable and disable lights, without the need of checking if all lights are on or off

The warn doesn’t work. It seems the previous lines are the problem. Ahhh I’m so tired I think I will change the each lights name to unique names and use if and

Scripting is tiring yeah. Choose what fits better for you :+1:
Wait for @ico4n code, seems like its building the entire system to do what you need

-- script.lua
-- first, i would recommend you placing all the baseParts with the lights into a folder then name it whatever you want
local lightsFolder:Folder = nil --[[ replace "nil" with the path to the folder
example if it's in your workspace then replace it with "workspace.<folder name here>"
]]
local probablity = 1 -- chance of all of the lights would turn off scaling up to 100
local duration = math.random(1, 7) -- random duration
local workings = {}

function index(t, va) -- for table.remove
    for i, v in pairs(t) do
        if v == va then
            return i
        end
    end
    return nil
end

function insertLight(part)
    if part:IsA("BasePart") then
        local light = v:FindFirstChildWhichIsA("SpotLight") -- replace SpotLight with any type of light if you're changing
        if light and light.Enabled then
           table.insert(workings, light)
        end
    end
end

for i, v in pairs(lightsFolder:GetChildren()) do
    insertLight(v)
end

lightsFolder.ChildAdded:Connect(insertLight)

task.spawn(function()
    while true do
        repeat
            task.wait()
        until #workings > 0 -- the code would stop working if there are no more lights in the folder
        task.wait(duration)
        local chosen = workings[math.random(1, #workings)]
        local chances = math.random(1, 100)
        if chosen then -- just in case if the object doesn't exist
           chosen.Enabled = false
        end
        table.remove(workings, index(workings, chosen))
        if chances <= probablity and #workings > 0 then -- if the chosen number is smaller than the given probablity
            for i, v in pairs(workings) do
                if v then
                    v.Enabled = false
                end
                table.remove(workings, index(workings, v))
            end
            -- insert a function here
        end
    end
end)

--[[
    if you like you could modify this code to fit your idea
    i would like to make a switch for you too but i don't have access to Studio rn, i wrote this on VSC
]]

point out any mistakes if possible lol, i wrote this on VSC since i don’t have access to Studio at this moment

@Dev_Peashie
if you don’t mind, can you test the code for me?

1 Like

Tested out your code and it does seem to work. I added a little bit of code to it so that it is clear when all the lights are off. The updated code is found below (almost identical to Remiski’s original code)

-- script.lua
-- first, i would recommend you placing all the baseParts with the lights into a folder then name it whatever you want
local lightsFolder:Folder = game.Workspace.Lights --[[ replace "nil" with the path to the folder
example if it's in your workspace then replace it with "workspace.<folder name here>"
]]
local probablity = 1 -- chance of all of the lights would turn off scaling up to 100
local duration = math.random(1, 7) -- random duration
local workings = {}

function index(t, va) -- for table.remove
	for i, v in pairs(t) do
		if v == va then
			return i
		end
	end
	return nil
end

for i, v in pairs(lightsFolder:GetChildren()) do
	if v:IsA("BasePart") then 
		local light = v:FindFirstChildWhichIsA("SpotLight") -- replace SpotLight with any type of light if you're changing
		if light and light.Enabled then
			table.insert(workings, light)
		end
	end
end

task.spawn(function()
	while true do
		repeat
			task.wait()
		until #workings > 0 -- the code would stop working if there are no more lights in the folder
		task.wait(duration)
		local chosen = workings[math.random(1, #workings)]
		local chances = math.random(1, 100)
		if chosen then -- just in case if the object doesn't exist
			chosen.Enabled = false
		end
		table.remove(workings, index(workings, chosen))
		if chances <= probablity and #workings > 0 then -- if the chosen number is smaller than the given probablity
			for i, v in pairs(workings) do
				if v then
					v.Enabled = false
				end
				table.remove(workings, index(workings, v))
			end
			-- insert a function here
		end
		if #workings == 0 then -- THIS DETECTS IT
			warn("All lights are off") 
		end
	end
end)

2 Likes

Same, I tested it and works great!
The detail, I understood from your explanation and OPs too. That should be a loop that has a chance to turn off all lights at the same time, and I assume constantly turn them on again to continue with the loop, sometimes one or more lights, and sometimes all lights should be turned off… haha but idk

1 Like

i’ve updated the code, adding a connection to automatically add another LightPart into the table once they’re parented into the folder with the print statement when all the lights are off too (@SeargentAUS)

mark this post as solution if it works

-- script.lua
-- first, i would recommend you placing all the baseParts with the lights into a folder then name it whatever you want
local lightsFolder:Folder = nil --[[ replace "nil" with the path to the folder
example if it's in your workspace then replace it with "workspace.<folder name here>"
]]
local probablity = 1 -- chance of all of the lights would turn off scaling up to 100
local duration = math.random(1, 7) -- random duration
local workings = {}

function index(t, va) -- for table.remove
    for i, v in pairs(t) do
        if v == va then
            return i
        end
    end
    return nil
end

function insertLight(part)
    if part:IsA("BasePart") then
        local light = v:FindFirstChildWhichIsA("SpotLight") -- replace SpotLight with any type of light if you're changing
        if light and light.Enabled then
           table.insert(workings, light)
        end
    end
end

for i, v in pairs(lightsFolder:GetChildren()) do
    insertLight(v)
end

lightsFolder.ChildAdded:Connect(insertLight)

task.spawn(function()
    while true do
        if #workings <= 0 then
            print("All lights are off, waiting for new child...") -- from @PoppyandNeivaarecute
            repeat
                task.wait()
            until #workings > 0 -- the code would stop working if there are no more lights in the folder
        end
        task.wait(duration)
        local chosen = workings[math.random(1, #workings)]
        local chances = math.random(1, 100)
        if chosen then -- just in case if the object doesn't exist
           chosen.Enabled = false
        end
        table.remove(workings, index(workings, chosen))
        if chances <= probablity and #workings > 0 then -- if the chosen number is smaller than the given probablity
            print("all lights turning off lol")
            for i, v in pairs(workings) do
                if v then
                    v.Enabled = false
                end
                table.remove(workings, index(workings, v))
            end
            -- insert a function here
        end
    end
end)

--[[
    if you like you could modify this code to fit your idea
    i would like to make a switch for you too but i don't have access to Studio rn, i wrote this on VSC
]]
1 Like

I tred using if and and for unknown reason it still doesn’t work. Idk what’s wrong with the script.

warn("This working")
if workspace.lamp.SurfaceLight.Enabled == false and workspace.lamp1.SurfaceLight.Enabled == false and workspace.lamp2.SurfaceLight.Enabled == false and workspace.lamp3.SurfaceLight.Enabled == false then
	warn('working ag')
	local cret = workspace.cret
			cret:SetPrimaryPartCFrame(CFrame.new(12.129, 2.665, -44))
			cret.sou:Play()
			wait(10)
			cret.sou:Stop()
			cret.sou.SoundId = "rbxassetid://9069546509"
			cret.sou.Volume = 2
	cret.sou:Play()
	wait(0.1)
			cret.hed.BillboardGui.Enabled = true
			wait(5)
			cret.sou:Stop()
			cret.sou.Volume = 1
			cret.sou.SoundId = "rbxassetid://2184044761"
			cret.hed.BillboardGui.Enabled = false
	cret:SetPrimaryPartCFrame(CFrame.new(0, -5, 0))
	end

The script stops at line 2 or 3. No errors.

In order for that to work, the if statement should be run everytime the lights changes, or at least in a constant loop. When are you running that code?

I insist that you should show the code you are using to turn on/off lights, thats when the “all lights turned off event” should happen. Not by trying to check when lights are off.

Can I use while true do with wait(1)?