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
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
There are a few issues with this script, fortunately they have nothing to do with collectionservice:
It says unknown global because you capitalized “light” just lowercase it and it will be fine.
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.
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!
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