How to make lights that you can shut on or off?

Hello there,
I’m currently building a runway for my group, Veloce. I have made decent use of the Roblox beam feature to create realistic lighting, in doing so I haven’t quite figured out how I can control these lights, as you would in real life.

If anybody has any advice or scripts I could plug into that would be awesome!

1 Like

CollectionService and a function to toggle the lights should be a good idea.

What is CollectionService?


It’s a service which you can add tags to objects. When tags are applied, you can use this service again to get all the objects tagged with the tag you named. Make sure to apply all objects to a name, or even parenting them to a folder.

After you use the CollectionService. Add a script that disables the lights, disables the beams but also turning off neon if necessary.

Oh, and unfortunately, this is not a category for free scripts. This is for assisting with scripting of an existing code.

Here’s a messy sample, it’s not the entire script:

local CollectionService = game:GetService("CollectionService")
local LightsEnabled = true

-- this is a for loop that isn't quite reliable at all scenarios
for i, descendant in pairs(folder:GetDescendants()) do -- assign folder with a folder holding all objects you want to toggle on and off
    CollectionService:AddTag(descendant, "toggle_a") -- name the tag here
end

local function ToggleLightsEnabled()
    for _, object in pairs(CollectionService:GetTagged("toggle_a")) do
        if LightsEnabled then
            if object:IsA("BasePart") then
                -- turn switch material to SmoothPlastic if necessary
            elseif object:IsA("Beam") then
                -- disable it
            elseif object:IsA("Light") then
                -- disable lights
            end
        elseif not LightsEnabled then
            if object:IsA("BasePart") then
                -- turn switch material to Neon if necessary
            elseif object:IsA("Beam") then
                -- enable it
            elseif object:IsA("Light") then
                -- enable lights
            end
        end
    end
    if LightsEnabled then -- outside loop
        LightsEnabled = false
    else
        LightsEnabled = true
    end
end

-- bind it to any form of button; UI, BasePart.Touched, ClickDetector etc.

Note: You could add up another function for the light toggles.


Also this is appears to be an XY problem. There’s a problem, the requester does not know how to do X. Then we have Y, but it doesn’t exist; no attempted solution is found. Missing the exact goal of scripting request.

Edit: Read @colbert2677’s post

4 Likes

Please read the guidelines of the Scripting Support category in the future before posting. Scripting Support is not a venue to ask for free code. Your request for advice, however, is valid.


There are many different numbers of ways to do this.

Typically you want to first get your structure down for this kind of a system. Ask yourself; are you looking for something primitive, or something complicated? Can each beam or light be a quick flip, or does it need to have special effects per type of effect?

If you’re looking for a primitive flicker, you can look up the CollectionService which allows you to utilise an assortment of API to group objects by a tag. Since each light-based effect has the Enabled property, you can set it straight away as well. Remember to add your tags manually in Studio.

If you’re interested in keeping track of tags, I’d recommend the Tag Editor plugin created by Tiffblocks.

-- Borrowing Operatik's code but this is better ;)
local CollectionService = game:GetService("CollectionService")
local LightsEnabled = true -- or false?

-- If you need to assign at runtime. Replace "container" with a model path. Workspace works too.
for n, descendant in pairs(container:GetChildren()) do
    if descendant:IsA("Beam") or descendant:IsA("Light") then
        CollectionService:AddTag(descendant, "LightObject")
    end
end

-- Enable function (does not handle BaseParts! write that yourself!!!)
local function ConfigureLights()
    LightsEnabled = not LightsEnabled
    for n, tagged in pairs(CollectionServicd:GetTagged("LightObject")) do
        tagged.Enabled = true
    end
end

This is obviously not complete code and it’s not in a style I’d like it in, though it gives you an example of how you’d structure such code. If I wrote the code for myself, it’d have a few more handlers included regarding handling initial and different states, not just a one-off function.

There are many other ways to achieve this desired functionality, including:

  • ModuleScripts
  • Folder-based hierarchies
  • Getting children or descendants of containers
  • CollectionService (above)

Look around the Developer Hub for potential items that might help you. It’d be great to know basic things, such as generic for loops, to help you achieve what you need.

I have not linked any articles because I am on mobile with low battery and it would help to search those up, in case you stumble across anything else of good use. I’m also lazy, it’s 3:55 AM.

3 Likes

That’s not what an XY problem is.

An XY problem is when the user is presented with problem X and they believe that solution Y will solve the problem, however solution Y doesn’t work so they ask for tips on how to fix solution Y while the root problem X is left unaddressed.

I wrote a whole thread regarding the XY problem that may be of use in understanding the concept. The linked website is one such attached resource.

2 Likes

Thanks for clarification, I’ll take this account to the notes for future reminders.

:dark_sunglasses:

Is the simplification of XY problem basically…?

X = ?
Y = (random solution; does not solve the problem X)
Asks Y, while X cannot be defined?

1 Like

You have a problem X, e.g. “The lights need to be able to switch off”

An XY problem would be if solution Y was already chosen “I’ll move the lights outside of workspace” and then ask us how to do that because they don’t know how, when a much more elegant solution (using a property like Enabled) is available and solves problem X much better.

In this thread the person asked for any solutions to switch off the lights, and therefore it’s not an XY problem, as you were given freedom to choose any solution you like.

3 Likes

I am totally a confused idiot, perhaps I misinterpreted the XY problem.

Off to reading it again. :frowning:

1 Like

From our good friend Wikipedia,

In my example, it would be XY if the person only asked about how to move things out of workspace, without any context of X, whilst the more effective and elegant solution of using Enabled was up for grabs if they had given context of the real problem.

Hope that makes sense :slightly_smiling_face:

4 Likes