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

Why won't my street lights turn on or off??

I am trying to turn my street lights on and off when it hits a certain time. I want them to turn on at 180000 and off at 6.65 there are no errors. It just doesn't work at all.

Script:

local onLight = script.Parent.StreetLightTimer.Lampstick.Lightpart.onLight

local offLight = script.Parent.StreetLightTimer.Lampstick.Lightingpart.offLight

while wait() do

if game.Lighting.ClockTime == 180000 then

	onLight.Enabled = true

	offLight.Enabled = true

		if game.Lighting.ClockTime == 6.65 then

		onLight.Enabled = false

		offLight.Enabled = false

		end

		end

end


2 answers

✓ Accepted answer

Use my answer from an old question I solved. Don't worry I explained everything in that question.

local onLight = script.Parent.StreetLightTimer.Lampstick.Lightpart.onLight
local offLight = script.Parent.StreetLightTimer.Lampstick.Lightingpart.offLight

local Lighting = game:GetService("Lighting")

local startOfDay = 6
local startOfNight = 18

local function checkIfDaytime(clockTime)
	if (clockTime >= startOfDay) and (clockTime < startOfNight) then -- if it's daytime
		return true
	elseif (clockTime >= startOfNight) or (clockTime < startOfDay) then -- if it's nighttime
		return false
	end
end

script:SetAttribute("Daytime", checkIfDaytime(Lighting.ClockTime)) -- creates the attribute
if script:GetAttribute("Daytime") == true then
	onLight.Enabled = false
	offLight.Enabled = false
else
	onLight.Enabled = true
	offLight.Enabled = true
end

script:GetAttributeChangedSignal("Daytime"):Connect(function()
	local attribute = script:GetAttribute("Daytime")
	if attribute == true then -- if it's true (day)
		onLight.Enabled = false
		offLight.Enabled = false
	else -- if it's false (night)
		onLight.Enabled = true
		offLight.Enabled = true
	end
end)

Lighting:GetPropertyChangedSignal("ClockTime"):Connect(function()
	local clockTime = checkIfDaytime(Lighting.ClockTime)
	
	script:SetAttribute("Daytime", clockTime)
end)

1 comment

0

Thank you so much I did not know loops don't work for this

I'm slightly confused, why make it happen at 180000? That cant happen in ClockTime. The most you can do is 18, and that works. Try this code:

local onLight = script.Parent.StreetLightTimer.Lampstick.Lightpart.onLight

local offLight = script.Parent.StreetLightTimer.Lampstick.Lightingpart.offLight

while wait() do

if game.Lighting.ClockTime == 18 then

    onLight.Enabled = true

    offLight.Enabled = true

        if game.Lighting.ClockTime >= 6.65 and game.Lighting.ClockTime <= 17.9 then

        onLight.Enabled = false

        offLight.Enabled = false

        end

        end

end

Hope this helps!

6 comments

0

Yay it now turns on I think I must of thought that timeofday and clocktime was the same the only issue is that it still won't turn off

0

i think its too exact, try using game.Lighting.ClockTime >= 6.65 and game.Lighting.ClockTime <= 17.9 then

0

ok i will try that in a bit

0

I tried using these signs < > each time I use them it gets underlined in red and also blue

0

try the code i edited in the answer

0

I tried it just now and it still the light won't turn off no errors

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