Light that turns on/off depending of the time

Hi! I’m working on a game that has a simple day/night cycle. I’ve made a script that turns on/off a light depending of the time of the day (6 AM and 6 PM) but it doesn’t work.

local light = script.Parent:WaitForChild("SpotLight")
local part = script.Parent

function NightTime()
	while true do
		light.Brightness = 2 -- turns on the light
		part.Transparency = 0
		wait(1)
	end
end

function DayTime()
	while true do
		light.Brightness = 0 -- turns off the light
		part.Transparency = 0.5
		wait(1)
	end
end

if game.Lighting.ClockTime > 18 then
	NightTime()
end

if game.Lighting.ClockTime > 6 then
	DayTime()
end

Note: it does work once if the game clock is already set to a time but won’t work afterwards. What’s wrong?

2 Likes

The script uses white true do, which makes it not work after the first time, it is like a loop, if you want it to work it makes a return when it has finished the cycle completely, but that is inefficient, what I recommend you would be to use TweenService, it is an optimal way to not use white true do and stay in a loop afterwards.

Also, I see you want to make it light up a little at a time, don’t you? Well TweenService can do the same, here you have your corrected functions with TweenService.

local TweenService = game:GetService("TweenService")
local InfoTween = TweenInfo.new(5, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut) -- Adjust it to your needs

function NightTime()
	TweenService:Create(light, InfoTween, { Brightness = 2 }):Play()
	TweenService:Create(part, InfoTween, { Transparency = 0 }):Play()
end

function DayTime()
	TweenService:Create(light, InfoTween, { Brightness = 0 }):Play()
	TweenService:Create(part, InfoTween, { Transparency = 0.5 }):Play()
end

you’re welcome :wink:

1 Like

Your script is performant heavy, why can’t you make one while loop and another script to check if it’s day or night, note that conditions are outside of the loop, soo it will run only once, you should make that:

game.Lighting.Changed:Connect(function()
if game.Lighting.ClockTime > 18 then

workspace.Lights.Transparency = 0
else

workspace.Lights.Transparency = 1
end
end)
while true do
game.Lighting.ClockTime+=step 

task.wait(cooldown) 

good solution too, it’s even easier than looping, i forgot about tween service because lerping are more efficient xd, but this is for another topic, good job

still doesn’t work… I’ve replaced my functions by yours and added the new variables TweenService and InfoTween. The light turns off/on once but won’t get triggered by the time

Look, in this part of your code

You can use white true do, I also see that you want to loop it.

This is how your final code will look like.

local light = script.Parent:WaitForChild("SpotLight")
local part = script.Parent

local TweenService = game:GetService("TweenService")
local InfoTween = TweenInfo.new(5, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut)

function NightTime()
	TweenService:Create(light, InfoTween, { Brightness = 2 }):Play()
	TweenService:Create(part, InfoTween, { Transparency = 0 }):Play()
end

function DayTime()
	TweenService:Create(light, InfoTween, { Brightness = 0 }):Play()
	TweenService:Create(part, InfoTween, { Transparency = 0.5 }):Play()
end

-- Note, I am assuming that it will repeat endlessly.
white wait(1) do -- I prefer to use "wait()" rather than "true", since it saves you a line.
	if game.Lighting.ClockTime > 18 then
		NightTime()
	elseif game.Lighting.ClockTime > 6 then -- Instead of having two separately, you can put them together.
		DayTime()
	end
end

That should solve some other inconvenience. If you have more problems (of the same script), let me know. :cool:

1 Like

It works very well. Thank you so much!

1 Like

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