Lights not working

Hey Everyone,
So for my story game I’m making it so that when the monster comes, the lights flash. However, I get no errors but nothing happens.
here is my script:

ReplicatedStorage = game:GetService('ReplicatedStorage')
LightsOnorOff = ReplicatedStorage:WaitForChild('EnabledLights')

LightsOnorOff:GetPropertyChangedSignal('Value'):Connect(function()
	if LightsOnorOff.Value == true then
		for _, light in pairs(game.Workspace:GetDescendants()) do
			if light:IsA('PointLight') then
				repeat
					light.Enabled = true wait(0.5) light.Enabled = false
				until LightsOnorOff.Value == false
			end
		end
	end
end)

Thanks for any help! The lights are supposed to flicker.

1 Like

The lights are supposed to flicker on and off.

1 Like

I think this might be a logic error.
You have a loop, which will carry out a repeat loop on every object if it is a point light.

This loop will make one light flicker until LightsOnorOff.Value == false.

But after this has happened, it will then continue the first loop to find the other PointLights.

I think you should switch the order in which the loops occur.

There are lots of possible ways which this could be corrected, with varying results

For example, a short version which wouldn’t guarantee an end state:

repeat
    for _, light in pairs(game.Workspace:GetDescendants()) do
		if light:IsA('PointLight') then
            light.Enabled = not light.Enabled 
		end
	end
    wait(0.5)
until not LightsOnorOff.Value

or we could take changing all the lights out as a function to allow easier custom behaviours:

function ChangeLightStatus(lightStaus)
    for _, light in pairs(game.Workspace:GetDescendants()) do
		if light:IsA('PointLight') then
            light.Enabled = lightStaus
		end
	end
end

LightsOnorOff:GetPropertyChangedSignal('Value'):Connect(function()
    if LightsOnorOff.Value then
        local nextStatus = true
        repeat
            ChangeLightStatus(nextStatus)
            nextStatus = not nextStatus 
        until not LightsOnorOff.Value
        ChangeLightStatus(true)
    end
end)

P.S. For booleans you don’t need to do == true and == false, they will directly evlaute to true or false without needing to do an == equality comparison.
You can use just .Changed for BaseValue objects instead of :GetPropertyChangedSignal() as these only fire when the value changes.

2 Likes

I tried the second one and I get this:

[16:40:55.783 - Workspace.Scripting.Script:16: Script timeout: exhausted allowed execution time

1 Like

Ok, That means that the loop is running for too long so has been stopped I forgot to add the wait into it.

after nextStatus = not nextStatus put the wait(0.5) and it should work.

2 Likes