How to make a function yield until a requiment is met for it to continue?

Hello, I need help with my project. I made a part that when touched by a player turns the clocktime to 0 (making it night time).

Here is the script:

local NightPart = script.Parent

function night()
	local Lighting = game:GetService("Lighting")
	Lighting.ClockTime = 0
	
end

NightPart.Touched:Connect(night)

Then, I made a light that shines brighter and brighter but I want it to only activate when the clocktime is 0 (when it’s night time). I am having issues, I am using a “repeat until” loop, but can’t figure out how to make the function pause an wait for the clocktime to be 0 to continue.

Here is the script:

local GlowingPart = script.Parent
local PointLight = GlowingPart.PointLight

local timeChange = 0.2
local BrightnessChange = 0.2

function Brightness()
	
	local Lighting = game:GetService("Lighting")
	if not Lighting.ClockTime == 0 then
		repeat 
			
		until Lighting.ClockTime == 0
	end
	

	for currentBrightness = 0, 20, BrightnessChange do
	PointLight.Brightness = currentBrightness
	task.wait(timeChange)
	end


end

Note: Script is not fully finished because I couldn’t find a way to make it wait till the clocktime is 0.

Can anyone assist me please?

2 Likes

I don’t think anything was wrong with your script.
It’s just that the repeat loop didn’t have a wait.

local GlowingPart = script.Parent
local PointLight = GlowingPart.PointLight

local timeChange = 0.2
local BrightnessChange = 0.2

function Brightness()

	local Lighting = game:GetService("Lighting")
	if Lighting.ClockTime ~= 0 then
		repeat
			task.wait()
		until Lighting.ClockTime == 0
	end

	for currentBrightness = 0, 20, BrightnessChange do
		PointLight.Brightness = currentBrightness
		task.wait(timeChange)
	end
end

Brightness()

3 Likes

That worked! Also, one more question, why can’t I use “not” here instead of the ~= sign?

Your script:

if Lighting.ClockTime ~= 0 then
		repeat
			task.wait()
		until Lighting.ClockTime == 0
	end

My script:

if not Lighting.ClockTime == 0 then
		repeat
			task.wait()
		until Lighting.ClockTime == 0
	end
2 Likes

When you do not Lighting.ClockTime == 0, the code will assume that it’s actually (not Lighting.ClockTime) == 0 which will just compare a boolean with a number, which is probably also why the game thought the ClockTime was 0.

Doing this will fix it

if not (Lighting.ClockTime == 0) then -- this works

end

It also tells you why it doesn’t work when you hover over it.
image

2 Likes

When you say that it is comparing a boolean and a number, do you mean that “(not Lighting.Clocktime)” will be either true or false and then we just have a 0, so that’s a number. If so, then how do you know if “(not Lighting.Clocktime)” is true or false. (I am assuming it’s true because you told me that the game thought the Clocktime was 0)

1 Like

It actually return false, you can test this for yourself via command bar.
This is also why it skipped the repeat loop and made the light lit up immediately.
image

2 Likes
if not game.Lighting.ClockTime == 0 then

-- equivalent to
if (not game.Lighting.ClockTime) == 0 then

-- equivalent to
if false == 0 then

-- to fix
if not (game.Lighting.ClockTime == 0) then

-- or
if game.Lighting.ClockTime ~= 0 then

The reason why it is false because numbers are truthy values, and not true is false.

2 Likes

Thanks, imma do this next time!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.