Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Code does not notice light is turned off?

Have I done something wrong? My goal here is when a light is turned off for more than between 30 and 50 seconds, a global variable will be set to true causing multiple other events to fire. I'm not having problems with the global variable, it's more that when I turn off the light, nothing happens. I put the print function in there to see if it's noticing that the light is turned off, but I never get the printed message in the output.

local blackFigureEvent = false
local Rand = Random.new()
while true do 
	wait(0.1)
	if workspace.HouseParts.BedLight1.LightPart.PointLight.Enabled == false then
		wait(0.1)
		print("Light is off")
		wait(Rand:NextNumber(30, 50));
		_G.blackFigureEvent = true
		print("Event activated")
	end
end

1 answer

✓ Accepted answer

The global variable is defined in the wrong place.

_G.blackFigureEvent = false
local Rand = Random.new()
while wait(0.1) do 
	if workspace.HouseParts.BedLight1.LightPart.PointLight.Enabled == false then
		wait(0.1)
		print("Light is off")
		wait(Rand:NextNumber(30, 50));
		blackFigureEvent = true
		print("Event activated")
	end
end

Tested and works. Also DO NOT use while true do since that can cause crashes.

1 comment

0

Thank you!

This discussion is preserved as it was. New answers, comments, and votes are closed.