Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Value change is not detected for some reason?

Asked by 2 years ago

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

1 answer

Log in to vote
0
Answered by 2 years ago
Edited 2 years ago

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()
0
Thank you very much! It works now plato2002 3 — 2y
0
Im really new to this website,do i need to somehow accept the answer or something like that? plato2002 3 — 2y
0
There should be an option next to my comment, I believe it says "Accept answer," or something along those lines. I'm not too sure, though. kodymarcey 49 — 2y
0
Also, you probably didn't see any errors because lines 5 and 8 in the second script were actually setting the locals, so printing the local would print "yes" or "no" (You also can't print an instance, it will give an error. It has to be some property of the instance). kodymarcey 49 — 2y
Ad

Answer this question