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

How to script a brick that randomly turns on at night and is off in the next night?

I'm trying to script a window that illuminates at night, I'm trying to make it so that math randomizes every day so that every night the window has a chance of illuminating or not, instead of staying on or off every single night for the rest of the game. It seems that that math and day works but the night section does not seem to work.

local window = script.Parent
local pointlight = window.Material
while true do
	wait(1)
	if game.Lighting:GetMinutesAfterMidnight() > 7 * 60 and game.Lighting:GetMinutesAfterMidnight() < 8 * 60 then
		local math = math.random(3,4)
		print(math)
end
	if game.Lighting:GetMinutesAfterMidnight() > 6 * 60 then
		print("day")
		window.Material = Enum.Material.Glass
		window.BrickColor = BrickColor.new("Black")
		window.Transparency = 0
	end
	if game.Lighting:GetMinutesAfterMidnight() > 18 * 60 and math ==3 then
		print("night")
		window.Material = Enum.Material.Neon
		window.BrickColor = BrickColor.new("Institutional white")
		window.Transparency = 0.1
	end
end

1 answer

✓ Accepted answer

Where you declared your math varable it might have a chance to be nil or no existant at some stage of your script. I've re-worked your code and dont worry about the Spawn() function i just used that to change the time of day for debugging.

Anyway the code:

local window = script.Parent
local pointlight = window.Material
local RandFunc = math.random
local Rnd = 4 

--[[
Delete this, this is just for debugging on my end, i needed something that would change the TOD
spawn(
	function() 
		while true do
			game.Lighting.ClockTime = game.Lighting.ClockTime+0.1
			wait()
		end
	end
)
]]

while true do
	wait(1)
	if game.Lighting:GetMinutesAfterMidnight() > 7 * 60 
		and game.Lighting:GetMinutesAfterMidnight() < 8 * 60 then
		Rnd = RandFunc(3,4)
		
	end
	print(Rnd)
	if game.Lighting:GetMinutesAfterMidnight() > 6 * 60 then
		print("day")
		window.Material = Enum.Material.Glass
		window.BrickColor = BrickColor.new("Black")
		window.Transparency = 0
	end
	if game.Lighting:GetMinutesAfterMidnight() > 18 * 60 and Rnd ==3 then
		print("night")
		window.Material = Enum.Material.Neon
		window.BrickColor = BrickColor.new("Institutional white")
		window.Transparency = 0.1
	end
	
	
end

Hope this helps! :)

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