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

How to make pointlights/spotlights bricks turn off and back on at a certain time of day?

Asked by
iBuizel 15
9 years ago

I am using your answers for my horror game I am planning on making. I am a bad scripter but very skilled in building. Any answer will help me lots, thank you.

2
You would need to use a script to gather all the lights in workspace and change their Enabled property to false (or true depending on the day). As well as use that script to detect the time from GetMinutesAfterMidnight. Without a attempt at a script, we can not help you. M39a9am3R 3210 — 9y
0
M39 above is right. However, instead of GetMinutesAfterMidnight, i would recommend you to check TimeOfDay in the (probably free-modeled) script you're using to cycle day-night. Marios2 360 — 9y
1
I wouldn't recommend to use TimeOfDay, unless if you know a little bit of string manipulation. Redbullusa 1580 — 9y
1
@Marios2 There is nothing wrong with freemodels. They are a great way to learn how to script. Validark 1580 — 9y

1 answer

Log in to vote
0
Answered by
hudzell 238 Moderation Voter
9 years ago

Firstly, before continuing, you should best have all the lights in one location in the Workspace. Maybe have a folder called "Lights" and put all of the lights you want to change in there. Here's a script to put in ServerScriptStorage or Workspace

local folderName = "Lights"
local folderLocation = game.Workspace
local lightName = "Light" --Name of all of the light instances (SpotLights, PointLights, SurfaceLights, etc.)
local turnOnTime = 1080 --When the light turns off (6:00PM)
local turnOffTime = 390 --When the light turns off (6:30AM)
--To get the time in minutes after midnight, just go into lighting, change the time of day to what you want to find, then
--input this in the command bar:
--print(game.Lighting:GetMinutesAfterMidnight())

--Variables, edit these if you have it set up differently.

game.Lighting.Changed:connect(function(change)
    if tostring(change) == "TimeOfDay" then
        local minutes = game.Lighting:GetMinutesAfterMidnight()
        if minutes < turnOnTime and minutes > turnOffTime then
            if folderLocation:FindFirstChild(folderName) == nil then warn('Warning! "'..folderName..'" does not exist in '..folderLocation.Name.."!") return end
            local x = folderLocation:FindFirstChild(folderName):GetChildren()
            for i = 1,#x do
                x[i][lightName].Enabled = false
            end
        else
            if folderLocation:FindFirstChild(folderName) == nil then warn('Warning! "'..folderName..'" does not exist in '..folderLocation.Name.."!") return end
            local x = folderLocation:FindFirstChild(folderName):GetChildren()
            for i = 1,#x do
                x[i][lightName].Enabled = true
            end
        end
    end
end)
0
What kind of script am I supposed to use, Local or just regular? And what do I do if I put "print(game.Lighting:GetMinutesAfterMidnight())" but no effect? I am sorry, I am a bit low-brained here. iBuizel 15 — 9y
0
Regular obviously. Since the script goes in server storage. hudzell 238 — 9y
Ad

Answer this question