Ad
Log in to vote
1

Code does not notice light is turned off?

Asked by 3 days ago

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

Log in to vote
1
Answered by 3 days ago

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.

0
Thank you! DiamondMiner199 30 — 3d
Ad

Answer this question