Bool value detection not working

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I’m trying to detect when a bool changes. (Could also work with a variable but I’d prefer to use a boolean)
  2. What is the issue? Include screenshots / videos if possible!
    when I run a script that changes a bool, the detector portion does not fire.

Script part that Should detect the change


script.Parent.ExteriorLighting.IsNightBool.Changed:Connect(function(NewValue)
	print(NewValue)
	print("Ha")
end)





script.Parent.ExteriorLighting.IsNightBool.Value = not script.Parent.ExteriorLighting.IsNightBool.Value

The rest of the relevant code

local IsNight = nil
local check = true

if  game.Lighting.ClockTime >= 6.5 and game.Lighting.ClockTime <= 17.7 then
	
	IsNight = false
	script.Parent.ExteriorLighting.IsNightBool.Value = false
	print(IsNight)
	print(script.Parent.ExteriorLighting.IsNightBool.Value)
	print(IsNight)
else

	IsNight = true
	script.Parent.ExteriorLighting.IsNightBool.Value = true
	print(IsNight)
	print(script.Parent.ExteriorLighting.IsNightBool.Value)
	print(IsNight)

end
	



while check == true do
	if  game.Lighting.ClockTime >= 6.5 and game.Lighting.ClockTime <= 17.7 then
		if IsNight == true then
			
			IsNight = false
			script.Parent.ExteriorLighting.IsNightBool.Value = false
			print(IsNight)
			print(script.Parent.ExteriorLighting.IsNightBool.Value)
			print("Changed from day to night")
		else
			print("I'm already")
			print(IsNight)
			print("sHOULD BE FALSE")
			wait(.2)
		end
	elseif game.Lighting.ClockTime <= 6.5 or game.Lighting.ClockTime >= 17.7 then
		if IsNight == false then
			
			IsNight = true
			script.Parent.ExteriorLighting.IsNightBool.Value = true
			print(IsNight)
			print(script.Parent.ExteriorLighting.IsNightBool.Value)
			print("changed from night to day")
		else
			print("I'm already")
			print(IsNight)
			wait(.2)
		end
	end
	wait(.1)
end


local descendants = game.Workspace.StreetLightsCombined:GetDescendants()


script.Parent.ExteriorLighting.IsNightBool.Changed:Connect(function(NewValue)
	print(NewValue)
	print("Ha")
end)

script.Parent.ExteriorLighting.IsNightBool.Value = not script.Parent.ExteriorLighting.IsNightBool.Value


  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

I found this post on the dev forum, but there seemed to be no clear solution

Do you get any errors in the output?

Your loop is pausing the thread

3 Likes

do you have any possible solution for this? It’s important for the script, but i don’t know how i’d have it run withoiut interfering.

Try wrapping the loop in a coroutine.

You could make the test work just by moving the while check do loop to the end of the file. Nothing after it will execute.

The correct way to do this is not to have the while loop at all, since any error that might get thrown inside the loop will stop it executing forever (the cause of many “dead servers” in Roblox games, when a dev uses a while true do for their game loop). Instead, you should run the code that’s in your while loop on Heartbeat. Your check code would look something like this (without all the unnecessary conditions and debug prints):

local RunService = game:GetService("RunService")
RunService.Heartbeat:Connect( function ( dt ) )
    script.Parent.ExteriorLighting.IsNightBool.Value = game.Lighting.ClockTime < 6.5 or game.Lighting.ClockTime > 17.7
end

A Changed event will only fire if the old value is not equal to the new one, so there is no need to check the current state of the value object, just set it. Setting it redundantly is faster than branching code.

Because this is a connecting an event handler now, and not entering an infinite loop, it will not prevent code after it from executing.

2 Likes

Sorry, but could you explain what the “dt” is? I’m new to using RunService, but it seems very useful

Nevermind I’ve figured it out! Thanks!