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

What is wrong with my SpotLight script? [UNANSWERED]

I added a night/day cycle script to my game, and it is working very good. So, I decided to add a light column (that works with SpotLight). Then I added a script to the SpotLight, this script is supposed to make the spot light opens in night(19:00:00) and close after 600 seconds(5:00:00). But the script is not working, here is the script:-

Lighting.Parent.TimeOfDay:connect(function(Time)
	if TimeOfDay = 19 then
		SpotLight = true
		while wait(600)
		SpotLight = false
	end
end

Here is the day/night cycle script if you want to know if it is wrong, note that I don't have troubles with this script:-

local dayLength = 24

local cycleTime = dayLength*60
local minutesInADay = 24*60

local lighting = game:GetService("Lighting")

local startTime = tick() - (lighting:getMinutesAfterMidnight() / minutesInADay)*cycleTime
local endTime = startTime + cycleTime

local timeRatio = minutesInADay / cycleTime

if dayLength == 0 then
	dayLength = 1
end

repeat
	local currentTime = tick()
	
	if currentTime > endTime then
		startTime = endTime
		endTime = startTime + cycleTime
	end
	
	lighting:setMinutesAfterMidnight((currentTime - startTime)*timeRatio)
	wait(1/15)
until false

NOTE: I am new to scripting, so you will find lots of mistakes.

Thanks for helping.

2 answers

In line 1 of your spotlight script, you connect to Lighting.Parent.TimeOfDay, but no event. You might want to try Lighting.changed:connect(, etc. In line two, you access the TimeOfDay property. That property is actually a string, so to compare, you should check if TimeOfDay equals '19:00:00'. A full spotlight script is provided below with other enhancements.

game.Lighting.changed:connect(function() --connect when a lighting property changes
	local myTime = game.Lighting.TimeOfDay --set myTime equal to lighting's TimeOfDay
	if myTime == '19:00:00' then --if its 1900 hours then
		SpotLight.enabled = true --enable the spotlight. Double check me if enabled is capitalized or not.
	else if myTime == '5:00:00' --if it's not 1900 then if it's 0500 then
		SpotLight.enabled = false --disable the spotlight
	end --close the if statement
end) --close the connect line

3 comments

0

Didn't work. I tried to put it in the part then put in the SpotLight itself and made some changes when it didn't work, I tried a lot of things but it is still not working. Try it yourself then edit your answer if it worked please. Thank you.

0

What didn't work? Were there errors or something going wrong?

0

I tried the script without changes as it is, when it didn't work I tried rewriting it. Here's what I found: 1)line 8, there shouldn't be ")" after "end" 2)There should be "then" somewhere in line 5 or 6. And is it even possible to make a light opens/closes at certain times?

I will add a 'while true do', since i know best using this one. So, add a while true do in a new script:

while true do

wait()
end

Okay, now let's make the part of disable and enable.

if game.Lighting.TimeOfDay == 19 then
SplotLight.Enabled = true
end
if game.Lighting.TimeOfDay == 5 then
SpotLight.Enabled = false
end

Try that, reply if it doesn't work and tell me what you see in output

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