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

Value change is not detected for some reason?

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

✓ Accepted answer

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()

4 comments

0

Thank you very much! It works now

0

Im really new to this website,do i need to somehow accept the answer or something like that?

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.

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).

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