Make light turn on at night using CollectionService

Hello there,

I’m working on a game right now, and I need some “support”.

I have this script here which controls the light parts to switch from neon to glass when it turns day. A fellow DevForum member already helped me with a part of this, so now I just need some help figuring out:

How can I make the script understand that the lights tagged as “BulbLight” are LIGHT INSTANCES?

I tried doing it myself like this;

local CollectionService = game:GetService("CollectionService")

local lightpart = game.CollectionService:GetTagged("Bulb")
local lightbulb = game.CollectionService:GetTagged("BulbLight")

local lightsOn = false
game.Lighting.LightingChanged:Connect(function()

	if game.Lighting:GetMinutesAfterMidnight() > 6 * 60 
		and game.Lighting:GetMinutesAfterMidnight() < 18 * 60 
		and lightsOn then
		lightsOn = false
		lightbulb.Enabled = false
		for _, lightpart in pairs (lightpart) do
		lightpart.Material = Enum.Material.Glass
		end
	elseif (game.Lighting:GetMinutesAfterMidnight() < 6 * 60 
		or game.Lighting:GetMinutesAfterMidnight() > 18 * 60)
		and not lightsOn then
		lightsOn = true 
		lightbulb.Enabled = true
		for _, lightpart in pairs (lightpart) do
		     lightpart.Material = Enum.Material.Neon
	    end
	end
end)

Well, as you probably expected it doesn’t turn on. I’m assuming the script doesn’t know what the light is, so it just doesn’t do anything?

Thanks for any help.

1 Like

Well Is the script in the player or the light?

2 Likes

It’s in ServerScriptService, it shouldn’t be local

The script works, it’s just that the lights INSIDE of the STREETLIGHTS won’t turn off and on.

1 Like

Could you show me a picture?‎ ‎ ‎ ‎ ‎ ‎

1 Like


test2

Let me know if you need any more pictures

1 Like

Okay, so I took a look at the code and I found a few issues.

First, you need to put game before the CollectionService.
Second, you need to make the script an LocalScript, because you are trying to get the service for the player.
Third, you have a misspelling on the tag.

So, your code should look like this;

local CollectionService = game:GetService("CollectionService")

local lightpart = game.CollectionService:GetTagged("Bulbs")
local lightbulb = game.CollectionService:GetTagged("BulbsLight")

local lightsOn = false
game.Lighting.LightingChanged:Connect(function()

	if game.Lighting:GetMinutesAfterMidnight() > 6 * 60 
		and game.Lighting:GetMinutesAfterMidnight() < 18 * 60 
		and lightsOn then
		lightsOn = false
		lightbulb.Enabled = false
		for _, lightpart in pairs (lightpart) do
		lightpart.Material = Enum.Material.Glass
		end
	elseif (game.Lighting:GetMinutesAfterMidnight() < 6 * 60 
		or game.Lighting:GetMinutesAfterMidnight() > 18 * 60)
		and not lightsOn then
		lightsOn = true 
		lightbulb.Enabled = true
		for _, lightpart in pairs (lightpart) do
		     lightpart.Material = Enum.Material.Neon
	    end
	end
end)
1 Like

So that means I also have to put the script somewhere outside of ServerScriptService, since it’s a local script, right?

This script doesn’t need to be a local script.

What is the lightsOn Variable doing for you? I don’t think it needs to be there.

Remove that variable and see if that fixes it - If not send me pictures of both and and night (with properties)

1 Like

it’s a variable to remember the state of the lights

i’ll try without it anyways

EDIT: after removing, nothing happens, so i definitely need it.

1 Like

You aren’t doing anything with that variable though other then setting it and checking the state - which you don’t want it to block it from changing the lights.

The elseif can be simplified to an else.

2 Likes

If it’s still not working, try using a breakpoint to debug where the logic isn’t lining up.

1 Like

A breakpoint is a marker to stop the execution at that point and focus it for you - used for debugging. You can add a breakpoint by clicking to the left of the script by the line number.

If you right click in that same area you can access different kinds of breakpoints - You only need the basic breakpoint here though.

2 Likes

When it’s paused, you can go into the script tab at the top and use the Step Into/Over/Out buttons to watch the script.
In the view tab, there’s also the watch button which will let you view the variables at play or specify ones to watch

2 Likes

do i just put a breaking point at the top of the variable? do i need to put a point on every line?

Just put one, at the point where the event is triggered

if game.Lighting:GetMinutesAfterMidnight() > 6 * 60 
2 Likes

1 Like

Yep, now just keep clicking on the Step Into button until you see the issue.
Make sure to use the watch menu to check the variables

Also just pointing out now, If you reuse something more then one (game.Lighting:GetMinutesAfterMidnight()) you should place it in a variable.

2 Likes

so how do i figure out if there’s an issue?

1 Like

Follow the logic in your mind

You want the lights to be off between 6-18 hours and on outside of that

First, set the time to be 700 (or somewhere else within that range) and make sure it goes through the logic to turn them off. After that, set it to 1900 and do the same test. It should turn them on

If it doesn’t, find out why.

1 Like

If it follows through it properly, check the properties on everything and make sure that it is turned off/on

1 Like