Help making lights go off when alien is released

Hello scripters! I am currently making a spaceship/core game where an alien will break loose of his cell every 5 minutes or so. I have the brightness set really low and lots of Spotlights/Pointlights but I’m not entirely sure how to make the lights go off (Or a really dark, dim red).
image
image
If anyone could help it would be greatly appreciated!

2 Likes
local LightsGroup = game:GetService("Workspace").Lights

for _, LightInstance in pairs(LightsGroup:GetDescendants()) do
	if LightInstance:IsA("SpotLight") or LightInstance:IsA("PointLight") then
		LightInstance.Color = Color3.fromRGB(255, 0, 0)
	end
end

it finds every spotlight/pointlight and sets the colour to red

2 Likes

WOW thanks your so good!!!

1 Like

How would I make the brightness darker?

2 Likes

Best off, on server load, getting this information immediately and storing it in a table for repeated referencing instead of constantly querying the workspace itself.

As for making the brightness darker, you’d change the SpotLight.Brightness property next to the color property.

2 Likes
LightInstance.Brightness = -- number here

How would incorporate

for _, LightInstance in pairs(LightsGroup:GetDescendants()) do 
		if LightInstance:IsA("Spotlight") or LightInstance:IsA("PointLight") then 
			LightInstance.Color = Color3.fromRGB(255,0,0)
		end
	end

In my script?
image

1 Like

I tried turning it into a function but that didn’t work. Any ideas?
image

local LightsGroup = game:GetService("Workspace").Lights
local Alien = game:GetService("Workspace").Alien
local Duration = 10
local IsOut = false

local function OnChange()
	if not IsOut then
		for _, LightInstance in pairs(LightsGroup:GetDescendants()) do
			if LightInstance:IsA("SpotLight") or LightInstance:IsA("PointLight") then
				LightInstance.Color = Color3.fromRGB(0, 255, 0)
				LightInstance.Brightness *= 2 
			end
		end
	else
		for _, LightInstance in pairs(LightsGroup:GetDescendants()) do
			if LightInstance:IsA("SpotLight") or LightInstance:IsA("PointLight") then
				LightInstance.Color = Color3.fromRGB(255, 0, 0)
				LightInstance.Brightness /= 2 
			end
		end
	end 
end

while true do
	task.wait(Duration)
	Alien:MoveTo(Vector3.new(-76.711, 308, 250))
	IsOut = true
	OnChange()
	task.wait(Duration)
	Alien:MoveTo(Vector3.new(-51.84, 309.86, 225.99))
	IsOut = false
	OnChange()
end

edit: if statement was inverted

3 Likes

A quick question. Is this in the server or client-sided?


Additionally, this is a modified variant of zoey’s script to account for initial indexing and storage of all references to lights:

This may or may not work first time.

-- Objects
local LightsGroup = workspace.Lights
local Alien = workspace.Alien
-- In a lot of cases, you do *need* game:GetService() but Workspace has it's own variable.

-- Tables
local Lights = {}

-- Settings
local Duration = 10

-- Internal
local IsOut = false

-- Functions
local function GetLights()
-- We have this to make sure we get *every* light and cache them in the Lights table.

	for _, LightInstance in pairs(LightsGroup:GetDescendants()) do
	-- Instance:GetDescendants() returns all children, children of children and so on.

		if LightInstance:IsA("Light")  then
		-- Light is the abstract/base class of all <...>Light object types. 
		-- Spotlight, PointLight and SurfaceLight will return true with :IsA("Light")

			table.insert(Lights, LightInstance)
			-- Add the Light Instance at the end of the table.
		end
	end
end

local function OnChange()
	if IsOut then
		for _, LightInstance in pairs(Lights) do
			LightInstance.Color = Color3.fromRGB(255, 0, 0)
			LightInstance.Brightness /= 2 
		end
	else
		for _, LightInstance in pairs(Lights) do
			LightInstance.Color = Color3.fromRGB(0, 255, 0)
			LightInstance.Brightness *= 2 
		end
	end 
end

-- Script Body
GetLights()  -- Run this at the start of the script to cache the lights.

while true do
	task.wait(Duration)
	Alien:MoveTo(Vector3.new(-76.711, 308, 250))

	IsOut = true
	OnChange()
	task.wait(Duration)

	Alien:MoveTo(Vector3.new(-51.84, 309.86, 225.99))
	IsOut = false
	OnChange()
end

Light - Base Class for all <…>Light object types.

2 Likes