Why aren't my windows working as intended?

When creating my windows, I needed to give them light as not all buildings leave their lights off when night falls and its still occupied. To this, I made a script that when the time reaches 17.8 (17:48 or 5:48 PM), the window lights should turn on. Of course, I can’t have every light on, so I wanted to make a randomizer where a window has a 1/128 chance of being turned on. However, when I do this, with the “wait(1)” at the end, the windows would end up flickering, so I needed to fix that, and to do so, I modified the script to look like this:

local light = script.Parent
local lightingpart = script.Parent.Parent

while true do
	times = game.Lighting.ClockTime
	loadshedding = math.random(1, 128)

	print("Loadshedding stage", loadshedding)
	if times >= 17.8 or times <= 6.3 and loadshedding > 65 then
		light.Enabled = true

		lightingpart.Material = "Neon"
		lightingpart.Color = Color3.fromRGB(171, 144, 85)

	else
		light.Enabled = false

		lightingpart.Material = "Glass"
		lightingpart.Color = Color3.fromRGB(4, 21, 53)
	end
	wait(15)
end

These were my results:

I’ve checked the console, too, as it should print a number and if the number is below 65, the window lights should be on

Regardless of the number, the window lights always stay on.

I’ve tried changing the “>” to a “<” and adding equal signs to them, and I’ve done several modifications that I haven’t recorded, but they turn up with either the same result where all the lights are on, or just breaks it

All I’m trying to do is make Windows that, every 9:00 AM (9:00), the script would choose a random number for each window to determine whether a window will be on from 17:48 to 6:18, and after that, a new number would be chosen

Since you’re using parts for the windows, try using brickcolor.new() instead of Color3.fromRGB()

An example would be something like this:

lightingpart.Color = BrickColor.new("Really Red")

That doesn’t do anything

Also that’s not what I was looking for

To clarify, do you want the light to have a 50% chance of being on when night comes and then turning off when it’s daytime?

1 Like

The problem is this line right here:

if times >= 17.8 or times <= 6.3 and loadshedding > 65 then

The way “or” works is that it splits the “if” statement into two pieces. The pieces here being:

if times >= 17.8

and

times <= 6.3 and loadshedding > 65

Because of this, if the “times” variable is greater than or equal to 17.8, the code doesn’t check the “loadshedding” variable at all. The fix here is pretty simple. You can add some parentheses to manipulate the way your if statement is split:

if (times >= 17.8 or times <= 6.3) and loadshedding > 65 then

I hope this helps! Have a great day.

Thanks, it worked, but now there’s the other thing I need to tackle

As you can see, every 15 seconds, the lights cycle, and what I want to do is to have any window with a loadshedding value greater than 65 to turn on for the duration of 17.8 to 6.3 (12 hours and 30 minutes in game time), then after that, a new number is chosen and that number will determine what lights will be on next time

I’ve already tried removing “wait()”, but that broke the windows

In order to stop the lights from toggling on/off multiple times per night (aka flicker), we need to have a variable for that. We can set this variable to “true” at the start of every night, so the lights will only be turned on/off once. Then, when the night ends, we can simply turn off the lights and set the variable to false.

local light = script.Parent
local lightingpart = script.Parent.Parent

local alreadyToggled = false -- Create an "alreadyToggled" variable to stop the lights from toggling multiple times per night.

while true do
	times = game.Lighting.ClockTime
	loadshedding = math.random(1, 128)

	print("Loadshedding stage", loadshedding)
	if times >= 17.8 or times <= 6.3 then -- Check if it's nighttime.
		
		if loadshedding > 65 and alreadyToggled == false then -- Check if loadshedding is greater than 65 and if we haven't toggled the lights already. If requirements are met, turn on the lights.
			
			alreadyToggled = true
			
			light.Enabled = true

			lightingpart.Material = "Neon"
			lightingpart.Color = Color3.fromRGB(171, 144, 85)
			
		elseif loadshedding <= 65 and alreadyToggled == false then -- Check if we haven't toggled the lights already even if the loadshedding value is less than 65. If the requirements are met, turn off the lights.
			
			alreadyToggled = true

			light.Enabled = false

			lightingpart.Material = "Glass"
			lightingpart.Color = Color3.fromRGB(4, 21, 53)
		end
	else -- Turn off the light and set alreadyToggled variable to false if it's not nighttime.
		
		alreadyToggled = false
		
		light.Enabled = false

		lightingpart.Material = "Glass"
		lightingpart.Color = Color3.fromRGB(4, 21, 53)
	end
	wait(1)
end

I hope this was helpful and not confusing.

Thanks, it helped, however, one thing I noticed is this:

Is this normal or is this script-related? All I have done is change the 65 to a 74

I am sorry, but I don’t see what the issue is in this picture. Could you explain it a little further?

Some windows that aren’t lit up still emit light

Weird. I tried to reproduce this issue, but it never happened. I do not think there is a problem with the script. Did you check if the unlit windows’ lights actually have their “Enabled” property set to true? If they are disabled, then it’s probably a lighting issue. You can try to play around with the lights’ properties or change the windows’ material to something other than glass. Not sure if those will do anything, but it’s the best I have. Apologies for not being able to provide a proper answer.

Huh. I can’t tell whether the unlit windows have their “Enabled” property set to true, but if it is a lighting issue, I don’t really know what’s causing this, as I have ShadowMap lighting enabled. Script should be the same, only the “Loadshedding” values have been set to a different number. This is especially notable up close and in game

Oh, I see what’s going on. For some reason, everytime I get closer to a window, this happens:

Up close:

Don’t know how to explain it, but for some reason, when I get up close to the building, some of the already lit windows decided that it changed its mind and goes unlit, but the lights are still emitted. I don’t know what’s causing this, but this is pretty annoying.

Especially if cosmic rays are causing this.

There might be a workaround for this. For example, instead of having PointLights inside of every window, you could create one PointLight, put it in ReplicatedStorage then clone & put it inside of lit windows, and when a window goes unlit, check if it has PointLight and if it does, destroy the PointLight.

The script would look like this (the script’s parent should be the window now since we won’t always have a light in the part and you should add a light instance into ReplicatedStorage):

local lightingpart = script.Parent
local light = game.ReplicatedStorage:WaitForChild("PointLight")

local alreadyToggled = false -- Create an "alreadyToggled" variable to stop the lights from toggling multiple times per night.

while true do
	times = game.Lighting.ClockTime
	loadshedding = math.random(1, 128)

	print("Loadshedding stage", loadshedding)
	if times >= 17.8 or times <= 6.3 then -- Check if it's nighttime.

		if loadshedding > 65 and alreadyToggled == false then -- Check if loadshedding is greater than 65 and if we haven't toggled the lights already. If requirements are met, turn on the lights.

			alreadyToggled = true

			if lightingpart:FindFirstChildWhichIsA("Light") == nil then -- Check if there is already a light instance under the part. If not, add one.
				
				local newLight = light:Clone()
				newLight.Parent = lightingpart
				newLight.Enabled = true
			end

			lightingpart.Material = "Neon"
			lightingpart.Color = Color3.fromRGB(171, 144, 85)

		elseif loadshedding <= 65 and alreadyToggled == false then -- Check if we haven't toggled the lights already even if the loadshedding value is less than 65. If the requirements are met, turn off the lights.

			alreadyToggled = true

			if lightingpart:FindFirstChildWhichIsA("Light") ~= nil then -- Check if there is a light instance under the part. If so, destroy it.
				lightingpart:FindFirstChildWhichIsA("Light"):Destroy()
			end

			lightingpart.Material = "Glass"
			lightingpart.Color = Color3.fromRGB(4, 21, 53)
		end
	else -- Turn off the light and set alreadyToggled variable to false if it's not nighttime.

		alreadyToggled = false
		
		if lightingpart:FindFirstChildWhichIsA("Light") ~= nil then -- Check if there is a light instance under the part. If so, destroy it.
			lightingpart:FindFirstChildWhichIsA("Light"):Destroy()
		end

		lightingpart.Material = "Glass"
		lightingpart.Color = Color3.fromRGB(4, 21, 53)
	end
	wait(1)
end

Well, thanks as it does work, however, once I remove the “print” line (as it does cause lag especially since I’m printing the same instance at least 1,000 times every second), the script doesn’t work

Are you sure you didn’t delete any other code by mistake? The print line shouldn’t affect the script at all, and when I deleted it, the script kept working with no problems.