Using collection service for lights, effects, etc

I want to use collection service on parts with a light (the child) inside, with this i want to have a single script that would enabled all lights when it turns to night and disabled when day, however im new to collection service so i dont really know how it works.
Also new with using lighting properties

Heres the code:

local CS = game:GetService("CollectionService")

for _, part in CS:GetTagged("NightLightPart") do
	
	local light = part.Light.Enabled
	
	if game.Lighting:GetPropertyChangedSignal("ClockTime") then
		if game.Lighting.ClockTime <= 6.1 then
			print("night")
			Light = true
		elseif 
			game.Lighting.ClockTime >= 6.1 then
			print("day")
			Light = false
		end
	end
end

Ive seen vids showing how to do it with the script in the part but i dont want there to be a script in each part that i want to light up at night and it just doesnt work using it in CS

It does print day but not night, and the light wont turn on at all.

You referenced the property, which means that, the value of the variable light, is a Boolean.

You are basically saying:

Boolean = true

The correct way is setting the light variable to the light object.
Then set its property to true/false.

So:

local light = part.Light

if day then
   light.Enabled = false
else
   light.Enabled = true
end


okay now it shows unknown global but idk how to

i tried this which worked before but i just dont get how CS works and how to identify things in it

wait i did cap letter for L let me see if it works now :skull:

No it didnt work

There are a few issues with this script, fortunately they have nothing to do with collectionservice:

  1. It says unknown global because you capitalized “light” just lowercase it and it will be fine.
  2. Did you actually tag your lights? You can do that by doing this:
CS:AddTag(instance_example, "NightLightPart:)

With this in mind here is a more efficient way to do you script:

local currenttime = "day" --or night

for i, v in pairs (workspace:GetChildren()) do
if v:FindFirstChild("Light") then
CS:AddTag(v.Light, "NIghtLightPart")
end

game.Lighting:GetPropertyChangedSignal("ClockTime"):Connect(function()
if game.LIghting.ClockTime <= 6.1 and currenttime ~= "night" then
currenttime = "night"
for i, v in pairs (CS:GetTagged("NightLightPart") do
v.Enabled = true
end
elseif currenttime ~= "day" then
currenttime = "day"
for i, v in pairs (CS:GetTagged("NightLightPart") do
v.Enabled = false
end
end
end)

Sorry for poor formatting, the tab button doesn’t work while making a post.

1 Like

Thanks bro i honestly thought id have to do it another way, i just changed a few things because when i tried it the lights kept turning on off on off and when the clock time hit 0 it turned off. It works just as i wanted now! :slight_smile:

local CS = game:GetService("CollectionService")

for i, v in pairs (workspace:GetChildren()) do
	if v:FindFirstChild("Light") then
		CS:AddTag(v.Light, "NIghtLightPart")
	end

	game.Lighting:GetPropertyChangedSignal("ClockTime"):Connect(function()
		if game.Lighting.ClockTime <= 6.1 then
			
			for i, v in pairs (CS:GetTagged("NightLightPart")) do
					v.Light.Enabled = true
			end
			
		elseif game.Lighting.ClockTime >= 6.1 then
			
			for i, v in pairs (CS:GetTagged("NightLightPart")) do
					v.Light.Enabled = false
			end
		end
	end)
end
1 Like

Oh, one thing I just noticed. The line

CS:AddTag(v.Light, "NIghtLightPart")

The tag name is miscapitalized. Just an FYI

1 Like

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