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