If statement passing when it shouldn't

I’m trying to run a script at any moment which checks the game’s ClockTime, but stops running after it has checked and passed the condition once, until it later needs to meet another.

To do this I tried to run an if statement, which checks the times in which I want an event to be run only once, to then run the same event with different parameters, to undo what was done when the new conditions are to be met, again only once.

The issue comes with the condition checks running once and only once. Instead, it continues to check if the conditions are met when it shouldn’t. Everything else works completely fine however.

The script is as follows:

local lighting = game:GetService(“Lighting”)
local db
local RS = game:GetService(“ReplicatedStorage”)
local events = RS.Events
local player = game.Players.PlayerAdded:Wait()

function toggleDB()
	local CT = lighting.ClockTime
	
	if CT <= 5.999 or CT >= 17.999 and not db then
		db = true
		print("Enable player nightvision")
		events.ToggleDB:FireClient(player,1)
		
	elseif CT >= 5.999 or CT <= 17.999 and db then
		db = false
		print("Disable player nightvision")
		events.ToggleDB:FireClient(player,0)
	end
end


lighting.LightingChanged:Connect(toggleDB)

db” is intended to stop the if statement passing anything after it, as “db” after the if statement has passed once should be true. If that is so, the only time something should run again, is when the ClockTime reaches “5.999” or above up. However, my console shows otherwise:

image

Funnily enough, I was able to get it to run as I intended it to when only one time was given, but doing so meant that players that would join in the middle of the night, past “17.999” , would have to wait until the next night for their ability to enable:

function toggleDB()
	local CT = lighting.ClockTime
	
	if CT <= 5.999 and not db then
		db = true
		print("Enable player nightvision")
		events.ToggleDB:FireClient(player,1)
		
	elseif CT >= 5.999 and db then
		db = false
		print("Disable player nightvision")
		events.ToggleDB:FireClient(player,0)
	end
end



lighting.LightingChanged:Connect(toggleDB)

image
image

I’m not too sure what’s going on here, but needless to say it’s confused me a little, as from my understanding, it shouldn’t be behaving the way it does, especially since it has worked how I intended it to under similar conditions.

It’s probably the <= and >= try using GetMinutesAfterMidNight.

This is a tutorial made by roblox for a day cicle

(7) Day/Night Cycle: Street Light - YouTube

You could modify it as you need.

You could use a return or break.
So basically:

function toggleDB()
	local CT = lighting.ClockTime
	
	if CT <= 5.999 or CT >= 17.999 and not db then
		db = true
		print("Enable player nightvision")
		events.ToggleDB:FireClient(player,1)
		return
	elseif CT >= 5.999 or CT <= 17.999 and db then
		db = false
		print("Disable player nightvision")
		events.ToggleDB:FireClient(player,0)
return
	end
end


lighting.LightingChanged:Connect(toggleDB)

Lua operator precedence places ‘and’ at a higher priority than ‘or’. Fix it with parentheses.

(CT <= x or CT >= y) and not db

2 Likes

This has brought me closer to the solution, as it fires once, only now it keeps switching between enabling and disabling the players ability whenever the time changes, disregarding the times entirely:

image

For that line you need ‘and’
elseif CT >= 5.999 and CT <= 17.999 and db then
Sorry for my delay, I was eating.

2 Likes

Using both of your suggestions, I managed to fix it. I’ve just been running a few tests to ensure it doesn’t fail at any point, though it doesn’t seem like it will at any point.

Here’s the final script:

local lighting = game:GetService("Lighting")
local db
local RS = game:GetService("ReplicatedStorage")

function toggleDB()
	local CT = lighting.ClockTime

	if (CT <= x or CT >= y) and not db then
		db = true
		. . .
		return
			
	elseif CT >= x and CT <= y and db then
		db = false
		. . .
		return
			
	end
end

Thank you all for the input!
*Editted to get rid of unnecessary variables

1 Like