Light toggle function not working

I am working on a trailer filming system for my Backrooms game and am currently trying to make the transition from normal to blackout and made this function to toggle a specific light

function lightOff(data)
	currentLight = game.Workspace.Map.Lights:FindFirstChild("Light"):HasTag(data)
	currentLight.Color = Color3.fromRGB(124, 112, 89)
	currentLight.SurfaceLight.Enabled = false
end

But when I use the function it does nothing and stops the script. I know the data variable is correct and have double checked that.

1 Like

you have to use tables for this, HasTag for one object is not enough to disable or turn on some of them, use this, and keep in mind that this

local ColorStates = {true = Color3.new(1,1,1),false = Color3.new(0,0,0)}
local function light(state:Boolean)

local Lights = workspace.Lights:GetChildren()
local chosen = Lights[math.random(1,#Lights)]

if chosen.SurfaceLight.Enabled ~= state then
chosen.Color = ColorStates[state]
chosen.SurfaceLight.Enabled = state
else
light(state)
end

end

I want the code to turn off a singular specific light that I’ve given the tag to.

1 Like

soo you can transform my script
what it does is check if light have opposite state to what we have and then change it

I don’t know how to do that. The issue is that the function I made breaks when it tries to select the light with the “trailer02” tag. I just want it to do that successfully.

1 Like

soo you have to use loops:

local all = game:GetService("CollectionService"):GetTagged("trailer02")

for i, v in all do
-- your function for each of lights

end

HasTag returns true or false if the Instance has the tag.
You could do something like this:

function lightOff(data)
    for _, light in ipairs(game.Workspace.Map.Lights) do
        if light:HasTag(data) then
            light.Color = Color3.fromRGB(124, 112, 89)
	        light.SurfaceLight.Enabled = false
            break
        end
    end
end

This should fix your issue!

1 Like

But that’s for multiple lights, I only want to turn off the one.

1 Like

Fixing indentation
######### ignore

Fixed the indentation, you’re good to go now.

soo why you have to find it with specific tag???, use loop for that or without it, collection service will do the job returning you table of instances, if it’s only one object, then collection service will return only it

Tutorial here: CollectionService | Documentation - Roblox Creator Hub

Returns this error.

1 Like

you’ve put instance, not table, you have to put table, not use loop if it’s only one object

I forgot to add :GetChildren() at the end
oops

I will re-write the code to use collection service now.

Where does the :GetChildren() go?

why do you making ipairs??? it’s not needed for this situation, you can do simply

for i,v in array do

function lightOff(data)
    for _, light in CollectionService:GetTagged(data) do
        light.Color = Color3.fromRGB(124, 112, 89)
	    light.SurfaceLight.Enabled = false
    end
end
1 Like

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