Make a part change material depending on the time

Hello there! I am working on making some low-poly builds. I want to make it so that once the sun sets, the material of some parts will change to be Neon, so that it will look like there are lights non. Then, once the sun rises, it will go back to smooth plastic. I know how to make it so the material changes, but I do not know what to put so that it detects the time and will continue on with the script. Please help!

1 Like

you will need to use os.time or os.date.
I tend to go with os.date as it offers more information.

Heres what I have done. Please make sure you understand anything you use.

function LocalTime()
	local Time = os.date("*t")
	local Year = Time["year"]
	local Month = Time["month"]
	local WDay = Time["wday"]
	local YDay = Time["yday"]
	local Day = Time["day"]
	local Hour = Time["hour"]
	local Minute = Time["min"]
	local Sec = Time["sec"]
 
	local DateData = {Time,Year,Month,WDay,YDay,Day,Hour,Minute,Sec}
	return DateData
end
 
function UTCTime()
	local Time = os.date("!*t")
	local Year = Time["year"]
	local Month = Time["month"]
	local WDay = Time["wday"]
	local YDay = Time["yday"]
	local Day = Time["day"]
	local Hour = Time["hour"]
	local Minute = Time["min"]
	local Sec = Time["sec"]
 
	local DateData = {Time,Year,Month,WDay,YDay,Day,Hour,Minute,Sec}
	return DateData
end
1 Like

I’m sorry, I am new to scripting. What is os.time / os.date?

so it basically gets like a table / dictionary of the time and date

Tables And dictionarys

Time and date

Okay, thanks. One last thing, I know I originally said I knew how to add the material script, but I am not so sure how to with this format. I know I am kind of asking you to write the whole script, but I am pretty new to all of this scripting.

Its fine it can be confusing.

You will most likely want to check the hours. so using the functions I gave before:

local LTime = LocalTime()
local CurrentHour = LTime[7]

if CurrentHour >= (Whatever Time you want) then
     local PartsList = game.Worspace.PartsThatWillChange
     for i = 1,#PartsList do
           PartsList[i].Material = Enum.Material.Neon
     end
end

Edit:
This will make the game do it based on real life time. if you just need the game time then that will just be in the game properties.

I’m sorry but I am crazy confused. I am just trying to make it so that it will detect the game TimeOfDay and change the material of script.Parent if the time is within a certain range.

i changed out the checker so its in game time.

if game.Lighting.TimeOfDay > MinimumTime and game.Lighting.TimeOfDay < MaximumTime then
     local PartsList = game.Worspace.PartsThatWillChange
     for i = 1,#PartsList do
           PartsList[i].Material = Enum.Material.Neon
     end
else
    local PartsList = game.Worspace.PartsThatWillChange
     for i = 1,#PartsList do
           PartsList[i].Material = Enum.Material.(Whatever it was originally)
     end
end

Also added in a thing so it will change the parts back after

2 Likes

Alright thank you so much for cooperating with me!

1 Like

Sorry for the late reply, but below is the script to how I want it. A am getting an output error saying “Attempt to compare number and string”

    if game.Lighting.TimeOfDay > 18 and game.Lighting.TimeOfDay < 6 then
local PartsList = script.Parent
for i = 1,#PartsList do
	PartsList[i].Material = Enum.Material.Neon
end
else
local PartsList = script.Parent
for i = 1,#PartsList do
	PartsList[i].Material = Enum.Material.SmoothPlastic
 end
end

My fauly I should have said earlier that TimeOfDay is a string.
You will need to use

local SplitVer = string.split(TimeOfDay,":") 
if tostring(SplitVer[1]) > 18 and tostring(SplitVer[1]) < 6 then

this will get the hours and turn it into a number that you can compare.

Okay, I did that but now it is saying that TimeOfDay (In the variable) is an unknown global.