Need help with night detector script

Hello,

I’m trying to make a script that changes a part’s properties when it is night time and then reverts them to their original settings when it becomes day time again. It is meant to work with an outside day/night script.
Here’s an image of what it currently looks like. Any help on what’s wrong and how I can fix it is greatly appreciated. Thanks!
NightLight

1 Like

Can you just detect the time with ClockTime?

Example:

if game.Lighting.ClockTime >= 17 then
-- thing
end
if game.Lighting.ClockTime >= 1 then
-- also thing
end
1 Like

lightpart.Color takes a Color3 value, not a string. You can learn more about that here.

That line should look like the following,

lightpart.Color = Color3.fromRGB(27, 42, 53)

lightpart.BrickColor takes a BrickColor value, not a string. You can learn more about that here.

That line should look like the following,

lightpart.BrickColor = BrickColor.new("Deep orange")

Here is some tips for performance. The time between lighting updates and part refreshing may not always align so you may see a slight delay when updating your part. This gets fixed with connections. In short, they “tell” us exactly when something happens. in this case, we are detecting when the time changes.

We can also make 6 * 60, 18 * 60, and Lighting variables because we know they will never change. (not property-wise)

Lastly, if the time updates and it is still morning, we do not need to update the part’s values which is done with the isDay variable.

local lightingService = game:GetService("Lighting") -- game.Lighting

local morningTime = 6 * 60
local nightTime = 18 * 60

local isDay = nil


local function updatePart()
	local currentTime = lightingService:GetMinutesAfterMidnight()

	if currentTime > morningTime and currentTime < nightTime then -- checks if time is between morning and evening
		if isDay ~= true then -- checks if it is not already morning
			isDay = true
			-- set your model to be morning
		end
	elseif isDay ~= false then -- checks if it is not already night
		isDay = false
		-- set your model to be night
	end
end


lightingService:GetPropertyChangedSignal("TimeOfDay"):Connect(updatePart) -- runs the code within updatePart() when Lighting.TimeOfDay is changed
updatePart() -- ypdates to make sure part matches with time of day when game loads

Code blocks, typed with 3 “" followed by another 3 ",” make copying code easy for developers so they cut put it into their code editors to review and change, try it out!

1 Like

Thanks a lot! I’m kind of stupid for not thinking of that.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.