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

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

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.

5 comments

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.

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.

1

I wouldn't recommend to use TimeOfDay, unless if you know a little bit of string manipulation.

1

@Marios2 There is nothing wrong with freemodels. They are a great way to learn how to script.

1 answer

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)

2 comments

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.

0

Regular obviously. Since the script goes in server storage.

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