How can I make a wall that spawns in when it is night time?

Hello! I am trying to make a wall that will appear when it is night time in the game. So when it is dark (night) the wall will be there and when someone trys to walk through they will get a notification saying, “this area is closed come back in the morning when it is open.” I need help with making the wall come when it is night time. Could anyone help me with this?

Things that I want it to have:
-When it is night time the wall will appear
-When someone trys to walk through a gui will pop up saying that the section is closed

1 Like

You can see the time in the lighting property just do something like

If game.Lighting.ClockTime == *your time* then
     Wall.CanCollide = true
     Wall.Transparency = 0
End

In a loop of course

1 Like

Instead of a loop, it’d probably be better to check the ClockTime property everytime it’s changed

game.Lighting:GetPropertyChangedSignal("ClockTime"):Connect(function()
     if game.Lighting.ClockTime == the time then
          Wall.CanCollide = true;
          Wall.Transparency = 0;
     end
end)
2 Likes

This wouldn’t work well, as ClockTime is the decimal form of TimeOfDay, meaning if it is using a day night cycle where it increments at decimal it could skip over this. It would just be better to create a threshold like this:

local Lighting = game:GetService('Lighting')
local Time_Min = 8
local Time_Max = 9

if Lighting.ClockTime <= Time_Min and Lighting.ClockTime >= Time_Max then
-- Do code.
end

and use something like RunService.Stepped to loop every frame to check if the time is within the specified time.

EDIT:
Using what Arc_Cosine said:

You could do something like this instead:

local Lighting = game:GetService('Lighting')
local Time_Min = 8
local Time_Max = 9

Lighting:GetPropertyChangedSignal("ClockTime"):Connect(function()
    if Lighting.ClockTime <= Time_Min and Lighting.ClockTime >= Time_Max then
        -- Do code.
    end
end)
4 Likes

Ok. Thank you for the help! I am going to try it out now.

So do I put it in the part or in ServerScriptService? I currently have it in the part.

If it is in the part then it would be something like this:

local Lighting = game:GetService('Lighting')
local Time_Min = 8
local Time_Max = 9
local Part = script.Parent

Lighting:GetPropertyChangedSignal("ClockTime"):Connect(function()
    if Lighting.ClockTime <= Time_Min and Lighting.ClockTime >= Time_Max then
        Part.CanCollide = true
        Part.Transparency = 0
    end
end)
1 Like

I am trying it and its not working for me. I have a day/night cycle and its using seconds. Does the code you sent me use minutes or seconds?

Here is the code to my current day/night cycle
local minutesAfterMidnight = 720
while true do
game.Lighting:SetMinutesAfterMidnight(minutesAfterMidnight)
minutesAfterMidnight = minutesAfterMidnight + 10
wait(10)
end

I completely forgot the fact that time loops over from 24 to 0, and I also mixed up the operators. Here should be a working, fixed version:

local Lighting = game:GetService('Lighting')

local Night_Max = 23.99 -- Max night value before loops to 0.
local Night_Min = 19 -- Minimum night value, meaning if it is equal to or greator than this number, it is night time.

local Morning_Min = 0 -- Minimum morning value, this is still technically night time.
local Morning_Max = 7 -- Maximum morning value, meaning if it is equal to or greater than this number, it is day time/morning
local Part = script.Parent

Lighting:GetPropertyChangedSignal("ClockTime"):Connect(function()
    if (Lighting.ClockTime >= Night_Min and Lighting.ClockTime <= Night_Max) or (Lighting.ClockTime >= Morning_Min and Lighting.ClockTime <= Morning_Max) then
        Part.CanCollide = true
        Part.Transparency = 0
    end
end)
1 Like

Do I keep my day/night cycle in the game too because when I am setting it to the night time it stays for a few seconds then changes to day time.

You could make your own system to manage that.

If night time stays for a few seconds, I would recommend getting a separate day night cycle. Either find one or make your own yourself. If you decide to find one yourself, be careful as many models contain viruses nowadays.

I currently have my own day/night system but when I have it set to be night time it will stay for a few seconds then change to day.

Here is my current day/night system

1 Like

No - that’s not a system whatsoever. That’s only a loop constantly updating a value which determines the time of the day within your game. When I say ‘system’, I mean a fully configurable manager for it.

Oh ok I will go make one quickly then.