Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to make it so that when it's night, the light turns off, but when it's day, it turns on?

Hello! So, I've been working on a place with a friend, and we're trying to make a ceiling light that turns off at night indicating that the store is closed, but turns on when it's day indicating that the store is open. There's no errors in the output, it just does nothing. Here's my current attempt at the script:

while wait(1) do
	if game.Lighting.ClockTime == 0 then
		script.Parent.PointLight.Enabled = false
		script.Parent.BrickColor = BrickColor.new("Really black")
	else
		script.Parent.PointLight.Enabled = true
		script.Parent.BrickColor = BrickColor.new("Institutional white")
	end
end

Any answer that actually works is fine.

(It's a workspace script inside of the light part)

1 answer

✓ Accepted answer

Assuming you have a script that is progressing the ClockTime of the game, here is what needs to be done. The issue was that your code is running on a loop that might not pick up when time = 0 due to the wait(1) and how quickly the ClockTime is actually ticking.

First, you can use Changed to run a function when any property of Lighting changes.

game.Lighting.Changed:Connect(function(changedProperty) --Lighting property is changed
	if changedProperty == "ClockTime" then --If ClockTime is changed, then run
		if game.Lighting.ClockTime == 0 then --Turn off light when ClockTime == 0
			script.Parent.PointLight.Enabled = false
			script.Parent.BrickColor = BrickColor.new("Really black")
		else --Turn on light if ClockTime ~= 0
			script.Parent.PointLight.Enabled = true
			script.Parent.BrickColor = BrickColor.new("Institutional white")
		end
	end
end)

4 comments

0

When I change the lighting via explorer, the light stays on. Or is this something that only works in game and not studio play?

0

Nevermind, I figured out that it's something that ONLY works in game. Thanks for the help!

0

The reason that is happening is because when you press play in studio, everything in the explorer is local to you, meaning if you change it while playing, the rest of the server won't see it. You can either press "Run" instead and change the time and the light will work when you change the time or you can create a little script that changes the time automatically.

0

Glad I could help! If you want the time to change automatically, something simple could be creating a script and putting this in it: while wait(1) do game.Lighting.ClockTime += 1 end

This discussion is preserved as it was. New answers, comments, and votes are closed.