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?
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)
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.
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.
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
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