You can write your topic however you want, but you need to answer these questions:
-
What do you want to achieve?
I have a generator system I made (really basic) and I would like to be able to turn on/off lights with it. For instance, if the generator is shut off, the lights would turn off as well. And vice versa for turning on.
-
What is the issue? The main issue is that I have lots of PointLight(s) and I don’t know how to reference all of them without making a whole lot of ‘local light917 = game.workspace’…etc…etc
-
What solutions have you tried so far? I looked here on the Dev Forum/Hub and all I found was a solution that utilized the Collection Service, however, I couldn’t figure out how to call it in a script.
Any help is appreciated. If anything wasn’t clear please let me know or DM me on Discord (@MicrosoftDeveloper1#5217)
You can loop through all of your instances and check for pointlights and add them to a table.
Or you can add a tag to it either manually or with a plugin. (This plugin works very well)
Once tagged, you can get all the items with a specific tag using CollectionService:GetTagged(stringname)
More info here
3 Likes
You want to look at loops
for i, v in ipairs(model:GetDescendants()) do
if v:IsA("PointLight") then
-- Turn Off or On Light
end
end
Could somebody explain for i, v in pairs – This explain pretty well how this type of loop works
2 Likes
where it says
...(model:GetDescendants()) do
specifically the ‘model’ part, can I do something like
...(game.Workspace.FacilityLights:GetDescendants()) do
or will it only work with a pre-defined variable?
Yes, you can use any variable you want, as long as it’s an array.
I’d highly recommend Tags instead of looping though. Its much faster, less costly on performance, and much more customizable. All you have to do is add a specific tag to your items.
Here is an example:
local CollectionService = game:GetService("CollectionService")
local FlickeringLights = CollectionService:GetTagged("Flicker")
local BlueLights = CollectionService:GetTagged("BlueLight")
local SwitchableLights = CollectionService:GetTagged("ToggleLight")
local FlickerTime = ( math.random(500,1200) ) / 100 --Generates a random time between 5 and 12 seconds.
local StartingSwitch = true --The starting position of switchable lights. On = true, Off = false
function StrobeLight(LightSource)
local OriginalBrightness = LightSource.Brightness
while wait(FlickerTime) do
if LightSource then
LightSource.Brightness = LightSource.Brightness * 0.5
wait(0.25)
LightSource.Brightness = OriginalBrightness
end
end
end
for i,v in pairs (FlickeringLights) do
coroutine.resume(coroutine.create(function() --Coroutine to make sure it doesn't hold up the loop.
StrobeLight(v)
end))
end
for i,v in pairs (BlueLights) do
v.Color = Color3.new(0,0,1) --Colors them blue
end
for i,v in pairs (SwitchableLights) do
v.Enabled = StartingSwitch
end
local CurrentSwitch = StartingSwitch --Make a currentswitch variable so the button event knows if its on or off
PlayerClickedButtonEvent:connect(function()
for i,v in pairs (SwitchableLights) do
v.Enabled = not CurrentSwitch --Applies the opposite value to the light.
end
CurrentSwitch = not CurrentSwitch --Applies the new value to the variable.
end)
3 Likes