Hello. I’m currently developing a lighting system but the script that handles turning the lights on and off depending on the switch state is not updating the value inside of the light itself.
Inside each light is a script and two Bool values. One is called “IsOn” which is responsible for controlling the individual lights in the fitting. Then there is another called “Switch” this value is changed between true and false by a script in the light switch. The individual lights and the switch script work fine. However the script that handles detecting when “Switch” has changed and to change the value of “IsOn” isn’t functioning correctly. I’ve already tried debugging by setting up prints inside of the if statements and functions to see if they were firing and they were. It’s just that the value “IsOn” seems to not get changed by it. Here is the script that I’ve named “MasterController” (I removed the prints after I diagnosed. For context the value “Power” that it refers to is so that it can be tied to a distribution board. The value exists and was True the entire time. This was intentional.)
--Fluorescent lamp master controller script. By Almonty7655
Power = game.Workspace:WaitForChild("Power")
function Switcher()
print(script.Parent.Switch.Value)
print(Power.Value)
if script.Parent.Switch.Value == true and Power == true then
script.Parent.IsOn.Value = true
else
script.Parent.IsOn.Value = false
end
end
script.Parent.Switch.Changed:Connect(Switcher)
Power.Changed:Connect(Switcher)
I really want to get this light system working ASAP so I can implement it in my game. Also to explain the use of the IsOn value. Then each fluorescent tube within the light fitting has it’s own script that listens for it. It works fine and am satisfied with it. Notice how I coded the light to have a chance of flickering on start up just like a real fluorescent lamp.
--Realistic fluorescent lamp script by Almonty7655
local MainsFreq60hz = false --If you want the buzz to be at 60Hz as opposed to 50Hz set this to true
local SP = script.Parent
local IsOn = script.Parent.Parent.IsOn
local Light = script.Parent.PointLight
local Ping = script.Parent.Sound
local Hum = script.Parent.Hum
local FlickerFactor = math.random(1,10)
Ping.PlaybackSpeed = math.random(0.9,1.2) --Sets a random pitch variation on the starter ping.
local Started = false
if MainsFreq60hz == true then Hum.PlaybackSpeed = 1.2 end
function Strike()
Ping:Play()
repeat wait()
SP.Material = "Neon"
Light.Enabled = true
local RNG = math.random(1,10)
if RNG < FlickerFactor then --RNG not passed. Light will flicker off and attempt to start again.
wait(math.random(0.05,0.25))
SP.Material = "SmoothPlastic"
Light.Enabled = false
Started = false
end
if RNG >= FlickerFactor then --RNG passed. Light is now truly on.
Started = true --This tells the script that we have now passed the RNG test.
end
until Started == true --Stop attempting to re-start the lamp once we pass the RNG test.
end
function Switch()
if IsOn.Value == true then --The light has been switched on.
local Warmup = math.random(0.25,1.5) --Pick a random time for this fluorescent lamp to start.
Hum:Play()
wait(Warmup) --Wait for the warmup time.
if IsOn.Value == false then return end --Checking if the switch has been turned off before the lamp finished warming up in order to try and prevent the lamp from being on when it shouldn't.
Strike() --Start the light.
end
if IsOn.Value == false then --Switch off the light.
Hum:Stop()
SP.Material = "SmoothPlastic"
Light.Enabled = false
Started = false
end
end
IsOn.Changed:Connect(Switch) --So the function will run when the value changes.
Edit: Here’s an RBXM of the entire model with scripts. Feel free to examine.
realistic fluorescent light.rbxm (38.4 KB)