So,I wanted to make a blackout event that occurs every like hour or so,but for some reason it doesnt detect when the value changes and the lights do not go off, i have no idea how to describe it more but heres the code in the lights
local pointlight = script.Parent.PointLight local LightPart = script.Parent LightPart.Material = "Neon" LightPart.BrickColor = BrickColor.new("Institutional white") local BlackoutValue = workspace.BlackoutTIme BlackoutValue.Changed:Connect(function() if BlackoutValue == "yes" then -- String value LightPart.Material = "Plastic" LightPart.BrickColor = BrickColor.new("Really black") pointlight.Brightness = 0 pointlight.Range = 0 elseif BlackoutValue == "no" then LightPart.Material = "Neon" LightPart.BrickColor = BrickColor.new("Institutional white") pointlight.Brightness = 1 pointlight.Range = 60 end end)
and the code in the ServerScriptService
local function BlackoutLoop() local BlackoutTime = math.random(2,5) -- 300,600 local TimeToBlackout = math.random (2,5) -- 900,3000 local blackoutValue = workspace.BlackoutTIme -- should be no or yes blackoutValue = "no" print("Blackout Ended!") wait(TimeToBlackout) blackoutValue = "yes" print("Blackout Started!") wait(BlackoutTime) BlackoutLoop() end BlackoutLoop()
Would really appreciate any suggestions or stuff like that Thank you
It seems that your issue is that you forgot to put .Value in the if statements. Besides that, I have some suggestions. First, I would set the BlackoutTime to be a BoolValue, it's a true or false value, rather then using a StringValue. Then, I would use task.wait(), it's more efficient (less throttling, I believe). Lastly, I would use :WaitForChild() with some of the locals. Here are my suggestions: (First script, the one in the part):
local pointlight = script.Parent:WaitForChild("PointLight ") local LightPart = script.Parent--this doesn't need :WaitForChild() because the script is loaded in after the part (the script is a child of the part). LightPart.Material = "Neon" LightPart.BrickColor = BrickColor.new("Institutional white") local Blackout = workspace:WaitForChild("BlackoutTime") -- I set this to a boolvalue and I used :WaitForChild(""), this makes sure that the value is loaded in before checking for it. This script may load in before the value does. Blackout.Changed:Connect(function() if Blackout.Value == true then LightPart.Material = "Plastic" LightPart.BrickColor = BrickColor.new("Really black") pointlight.Brightness = 0 pointlight.Range = 0 elseif Blackout.Value == false then LightPart.Material = "Neon" LightPart.BrickColor = BrickColor.new("Institutional white") pointlight.Brightness = 1 pointlight.Range = 60 end end)
(Second script, the one inside ServerScriptService):
local function BlackoutLoop() local BlackoutTime = math.random(2,5) -- 300,600 local TimeToBlackout = math.random (2,5) -- 900,3000 local blackoutValue = workspace:WaitForChild("BlackoutTime") -- this will be true or false blackoutValue.Value = false print("Blackout Ended!") task.wait(BlackoutTime) -- task.wait() is better to use, according to what i searched up blackoutValue.Value = true print("Blackout Started!") task.wait(BlackoutTime) BlackoutLoop() end BlackoutLoop()