Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

What is wrong with my SpotLight script? [REPOSTED TWICE]

Asked by 9 years ago

Reposting reason: The last 2 times, the answers I was given were all INCORRECT.

So... after I added a day/night cycle script to my game, I made a light column with SpotLight. Then I added a script to the SpotLight to make it opens at a specific time(19:00:00) and closes at(5:00:00). The script is not working, here is it:-

game.Lighting.Changed:connect(function()
    local myTime = game.Lighting.TimeOfDay
    if myTime == "19:00:00" then
        SpotLight.enabled = true
    else if myTime == "5:00:00" then
        SpotLight.enabled = false
    end
end

And here is the day/night cycle script if you think there's something wrong in it, 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: Please tell in your answer the following:-

1.Shall I put this script in normal script or in local script.

2.Shall I put this script in the SpotLight itself or in the part which has the SpotLight in.

3.Explain what did you change.

4.Should I use PointLight instead.

Thanks for helping.

1 answer

Log in to vote
1
Answered by 9 years ago

I think this will be fine in a normal script. The glaring error I have found, unless I have missed something, is that SpotLight is not referenced. You need to locate the spotlight first.

If this script was in the spotlight then it would be:

game.Lighting.Changed:connect(function()
    local myTime = game.Lighting.TimeOfDay
    if myTime == "19:00:00" then
        script.Parent.enabled = true
    else if myTime == "5:00:00" then
        script.Parent.enabled = false
    end
end

A spotlight is probably best too, but you can use PointLights too

0
It didn't work, but you helped me in knowing some new things, upvoted! kudorey619 138 — 9y
Ad

Answer this question